This article analyzes the usage of PHP string interception function. Share to everyone for your reference. The specific analysis is as follows:
PHP with the function of intercepting strings can only handle English, the number can not intercept Chinese mixed row, the following example is easier to use, the first is mainly for beginners to learn, the specific code is as follows:
Copy Code code as follows:
<?php
Construct string
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
echo "Original string:<b>". $str. " </b><br> ";
Interception in various ways
$str 1 = substr ($STR, 5);
echo "starts at 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 "takes the penultimate 5 characters:". $str 3. " <br> ";
$str 4 = substr ($str, -8,4);
Echo takes 4 characters backwards from the penultimate character: ". $str 4." <br> ";
$str 5 = substr ($str, -8,-2);
Echo begins with the last 8th character until the penultimate character: ". $str 5." <br> ";
?>
Support for mixed interception in Chinese and English, the code is as follows:
Copy Code code as follows:
<?php
/*
------------------------------------------------------
Parameters:
$str _cut A string that needs to be truncated
$length maximum length allowed for string display
Program function: Intercept all corners and half-width (Chinese and English) mixed strings to avoid garbled
------------------------------------------------------
*/
function Substr_cut ($str _cut, $length)
{
if (strlen ($str _cut) > $length)
{
for ($i =0; $i < $length; $i + +)
if (Ord ($str _cut[$i]) > 128) $i + +;
$str _cut = substr ($str _cut,0, $i). ".";
}
return $STR _cut;
}
?>
I hope this article will help you with your PHP program design.