Home >Backend Development >PHP Tutorial >JS intercepts strings. Common methods for intercepting strings.
1: explode splits the string
eg:
$str = xxx:abc;
$abc = explode(':','$str'); //What is obtained is an array
echo $abc[ 1]; //Output abc
Disadvantages: You must know the split point and the location of the required data
Advantages: Easy to understand
2: str_replace replaces the string part
eg:
$str = abcdefg;
$abc = str_replace('defg','','$str'); //Replace defg with empty
echo $abc; //Output abc
Disadvantages: The replaced part must be known
Advantages: Good Understanding
3: substr traverses the string
eg:
$str = abcdefg;
$re = array();
for($i = 0;$i < strlen($str); $ i++){ //Convert $str into an array
$re[] = substr($str,$i,1);
}
echo $re[0].$re[1].$re[ 2]; //Output abc
Disadvantages: Must know the entire string, low efficiency
Advantages: Splicing at will
The above introduces the common methods of intercepting strings with js, including the content of intercepting strings with js. I hope it will be helpful to friends who are interested in PHP tutorials.