php's built-in function for string manipulation 2

1. Get the string: substr($str, $start, $length)

<? php
 $email = '[email protected]' ;
 echo 'original string: '. $email ; echo '<hr>' ;
 // Starting from 0, get 7 characters 
echo  substr ( $email , 0,7); echo '<hr>' ;
 // -3: Negative numbers count from the end 
echo  substr ( $email ,-3,3); echo '<hr>' ;
 ?>

 

2, the replacement string: substr_replace()

<? php
 $email = '[email protected]' ;
 echo 'original string: '. $email ; echo '<hr>' ;
 // Note that negative numbers are different from positive numbers, negative numbers start from -1, Positive numbers start from 0 
echo  substr_replace ( $email ,'163',9,2); echo '<hr>' ;
 echo  substr_replace ( $email ,'cn',-3,3); echo '<hr>' ;

// Set the string length to 0 to achieve insertion without deletion 
echo  substr_replace ( $email ,'_zhu',8,0); echo '<hr>' ;
 // Set an empty string to achieve insertion deletion 
echo  substr_replace ( $email , '', 0,4); echo '<hr>' ;
 // insert characters at the beginning 
echo  substr_replace ( $email , 'My mailbox:', 0,0); echo '< hr>' ;
 ?>

 

3, substr_count(): the frequency (number of times) of the query string  

<?php
$email = 'My blog name is cl94,welcome to my blog';
echo '原始字符串:'.$email; echo '<hr>';
echo substr_count($email,'blog',0,10); echo '<hr>';
?>

 

4, substr_compare($str, $str1, startpos ): compare strings

 

<? php
 $str1 = 'My blog name is cl94,welcome to my blog' ;
 $str2 = 'My blog' ;
 // startpos: specifies where in string1 to start the comparison. If negative, count from the end of the string.
//Return result (0: equal; <0:$str1<$str2; >0:$str1>$str2);
//Compare the ascii value 
echo  substr_compare ( $str1 , $str2 ,0); echo '<hr>' ;

echo substr_compare($str1, $str2,0,7);echo '<hr>';

?>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326104403&siteId=291194637