Php-string interception of specific characters before or after

1.php intercepts the content behind specific characters  

You can use the function strripos to get the first occurrence of a string in another string.

$number = '1_0';

$result = substr($number,strripos($number,"_")+1);
echo $result;

 

 Result output 0

 
2. PHP intercepts the content in front of a specific character
You can use the function strrops to get the last occurrence of a string in another string.
$number = '1_0';
$result = substr($number,0,strrpos($number ,"_"));
echo $result;

 

Result output 1

 

Ps: substr ($ number, strripos ($ number, "_") + 1) where +1 1 is the next 1 byte, if it is Chinese, a Chinese character +3, Chinese characters in the string account for three bytes , If you add +1 on the basis of Chinese characters, it will be garbled.

Reference: https://www.cnblogs.com/afeige/p/11753003.html

You can use the function strripos to get the first occurrence of a string in another string.

$number = '1_0';

$result = substr($number,strripos($number,"_")+1);
echo $result;

 

 Result output 0

 
2. PHP intercepts the content in front of a specific character
You can use the function strrops to get the last occurrence of a string in another string.
$number = '1_0';
$result = substr($number,0,strrpos($number ,"_"));
echo $result;

 

Result output 1

 

Ps: substr ($ number, strripos ($ number, "_") + 1) where +1 1 is the next 1 byte, if it is Chinese, a Chinese character +3, Chinese characters in the string account for three bytes , If you add +1 on the basis of Chinese characters, it will be garbled.

Reference: https://www.cnblogs.com/afeige/p/11753003.html

Guess you like

Origin www.cnblogs.com/T8888/p/12691994.html