Home  >  Article  >  Backend Development  >  PHP - How to use iconv_substr() function to intercept part of a string?

PHP - How to use iconv_substr() function to intercept part of a string?

WBOY
WBOYforward
2023-09-05 20:01:021543browse

PHP - 如何使用iconv_substr()函数截取字符串的一部分?

In PHP, the iconv_substr() function is used to cut a part of the specified string through the offset and length parameters. Suppose we have a string "helloWorld" and we just want to cut and display the string (llowo) then we will use a number between 2 and 5 to select it .

Syntax

string iconv_substr(str $string, int $offset, int $length, str $encoding)

Parameters

iconv_substr()Accepts four parameters: $string, $offset, $length and $encoding.

  • $string− The $string parameter specifies the original encoding

  • $offset− If $ If the offset parameter is non-negative, the iconv_substr() function will cut the selected part of the string starting from the offset character, counting from zero. If negative, the iconv_substr() function cuts the portion starting at that position, offset by characters from the end of the string.

  • $length− If the $length argument is given and is positive, the return value contains at most length characters starting at offset .

  • $encoding− If the encoding argument is absent or null, the string is assumed to be in iconv.internal_encoding.

Return value

iconv_substr()The function returns the portion of the string specified by the offset and length parameters. Returns False if the string is shorter than the offset characters. If the string is exactly the same length as the offset characters, null or an empty string will be returned.

Example 1

icov_substr() function read without spaces

Live demonstration

<?php
   // Helloworld sting is used
   // To cut the selected portion from string
   //iconv_substr function is used
   $string = iconv_substr("HelloWorld!", 2, 7, "UTF-8");

   // It will returns the character from 2 to 7
   var_dump($string);
?>

Output

string(7) "lloWorl"

Example 2

icv_substr() function with space reading

Real-time demonstration

<?php
   // Helloworld sting is used
   // To cut the selected portion from string
   //iconv_substr function is used
   $string = iconv_substr ("Hello World!", 2, 7, "UTF-8");

   // It will returns the character from 2 to 7
   var_dump($string);
?gt;

Output

string(7) "llo Wor"

The above is the detailed content of PHP - How to use iconv_substr() function to intercept part of a string?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete