Home > Backend Development > PHP Tutorial > 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
Release: 2023-09-05 20:10:01
forward
1616 people have browsed it

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)
Copy after login

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 .

  • < li>

    $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);
?>
Copy after login

Output

string(7) "lloWorl"
Copy after login

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;
Copy after login

Output

string(7) "llo Wor"
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template