This article mainly introduces the PHP string interception function usage analysis, gives two examples, respectively, the use of PHP's own string interception function and custom function to implement the method of string interception, is a very useful string manipulation skills,
This paper analyzes the usage of PHP string interception function. Share to everyone for your reference. The specific analysis is as follows:
PHP's own function of intercepting strings can only handle English, the number can not intercept the Chinese mixed, the following example is more useful, the first is mainly for beginners to learn to use, the specific code is as follows:
<?php //construct string $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; echo "Original string:<b>". $str. " </b><br> "; Interception by various means $str 1 = substr ($STR, 5); echo "starts from the 5th character to the last:" $str 1. " <br> "; $str 2 = substr ($str, 9,4); echo "takes 4 characters starting from the 9th character:". $str 2. " <br> "; $str 3 = substr ($str, -5); echo "Take the countdown 5 characters:". $str 3. " <br> "; $str 4 = substr ($str, -8,4); echo "takes 4 characters backwards from the last 8th character:". $str 4. " <br> "; $str 5 = substr ($str, -8,-2); echo "starts from the 8th character to the bottom of the 2nd character:". $str 5. " <br> "; ? >
Support Chinese and English mixed interception, the code is as follows:
<?php/*------------------------------------------------------Parameters: $str _ Cut the string that needs to be truncated $length allow the maximum length of the string to display the program function: Intercept full-width and half-width (kanji and English) mixed strings to avoid garbled characters------------------------------------------------- -----*/Function Substr_cut ($str _cut, $length) {if (strlen ($str _cut) > $length) {for ($i =0; $i < $l Ength; $i + +) if (Ord ($str _cut[$i]) > + $i + +; $str _cut = substr ($str _cut,0, $i). "..."; } return $STR _cut; };