PHP Intercept String function (Chinese string) This is a PHP intercept string function Oh, this is a support Chinese string oh, it can intercept HTML and Chinese and Western text, and so on mixed content, and the HTML tag is not counted within the character interception, if the HTML tag is not closed, The program will automatically filter out excess labels.
PHP Tutorial Intercepting String Functions (Chinese strings)
This is a PHP intercept string function Oh, this is a support Chinese string oh, it can intercept HTML and Chinese and Western text, and so on mixed content, and the HTML tag is not counted within the character interception, if the HTML tag is not closed, the program will automatically filter out the extra tags.
*/
function Mysubstr ($STR, $length) {
$tagcnt = 0;
$charcnt = 0;
$tag = ";
$maxlen = strlen ($STR);
$resultstr = ";
$tagstack = Array ();
for ($i = 0; $i < $length; $i + +) {
if ($str [$i] = = < ') {
$resultstr. = $str [$i];
for ($j = $i; $str [$j]!= ' > '; $j + +, $length + +) {
$tag. = $str [$j];
}
$tagcnt + +;
$length + +;
$tag. = ' > ';
If it is the start tag, then the stack will be stacked if it is the closing tag corresponding to it.
if (Preg_match ('/< ([^/]+) >/i ', $tag, $r)) {
Echo ' into the stack: ', Htmlspecialchars ($r [1]), '
';
Array_push ($tagstack, $r [1]);
}
ElseIf (Preg_match ('/') $tagstack [Count ($tagstack)-1]. ' /', $tag)) {
Echo ' Out stack: ', Htmlspecialchars ($tagstack [Count ($tagstack)-1]), '
';
Array_pop ($tagstack);
}
$tag = ";
Continue
}
$charcnt + +;
$resultstr. = $str [$i];
}
Echo '
The final result is: ';
Stack is empty for direct return
if (empty ($tagstack)) {
return $resultstr;
}
Otherwise, remove the start tag without the end tag
else{
while (!empty ($tagstack)) {
$tag = Array_pop ($tagstack);
$index = Strrpos ($resultstr, $tag);
for ($i = $index-1; $resultstr [$i]! = ' > '; $i + +) {
$RESULTSTR [$i] = ';
}
$resultstr [$i + +] = ';
}
return $resultstr;
}
}
$sttime = Microtime (true);
$stmem = Memory_get_usage ();
$str = "a1b2
C3D4e5
F6G7h8 ";
echo ' Processing results are:
', Htmlspecialchars (Mysubstr ($STR, 18)), '
';
echo "Memory Usage:", (Memory_get_usage ()-$stmem), '
';
echo "Algorithm run Time (microtime):", (Microtime (True)-$sttime), '
';
Method Two
/
**
* Function name Html_substr
* Function intercepts a specified length of string from the HTML string, and the HTML tag is not counted.
* Parameters
* $str the string to be intercepted
* $len the length to intercept
* $mode the processing of mismatched marks 0 by deleting (default), 1 padded
* Return to the intercepted string
* Description
* Multibyte characters are not considered, only bytes are counted units
* Separate tags are not considered
**/
function Html_substr ($str, $len, $mode =0) {
$ar = Preg_split ('/( |<[^>]*>)/s ', $str,-1, preg_split_delim_capture);
foreach ($ar as $k = = $v) {
if ($v {0}! = ' < ') {
$len = $len-strlen ($v);
if ($len < 0) $ar [$k] = substr ($v, 0, $len);
}else $ar [$k] = Strtolower ($v);
if ($len <= 0) break;
}
$ar = Array_slice ($ar, 0, $k + 1);
$len = count ($ar);
foreach ($ar as $k = = $v) {
if ($v {0} = = ' < ' && $v [1]! = '/') {
$ch = Str_replace (' < ', ' for ($i = $k +1; $i < $len && $ar [$i]!= $ch; $i + +);
if ($i = = $len)
if ($mode)
$ar [$len] = $ch. $ar [$len];
Else
$ar [$k] = ';
}
}
return join (' ', $ar);
}
$STR = "123ABC456def789 ";
Echo '
<xmp> '; &lt;br/&gt;echo html_substr ($STR, 5). Php_eol;&lt;br/&gt;echo Html_substr ($STR, 5, 1); &lt;/p&gt;&lt;/blockquo Te&gt;&lt;p&gt;1 2 &lt;/p&gt;&lt;p align= "left" &gt;&lt;span id= "url" itemprop= "url" &gt;http://www.bkjia.com/phpjc/ 632042.html&lt;/span&gt;&lt;span id= "Indexurl" itemprop= "Indexurl" &gt;www.bkjia.com&lt;/span&gt;&lt;span id= " Isoriginal "itemprop=" isoriginal "&gt;true&lt;/span&gt;&lt;span id=" Isbasedonurl "itemprop=" IsBasedOnUrl "&gt;http ://www.bkjia.com/phpjc/632042.html&lt;/span&gt;&lt;span id= "Genre" itemprop= "genre" &gt;TechArticle&lt;/span&gt; &lt;span id= "description" itemprop= "description" &gt;php intercept string function (Chinese string) This is a PHP intercept string function Oh, this is a support Chinese string oh, it can intercept HTML and Chinese and Western text, etc. mixed content, ... &lt;/span&gt;&lt;/p&gt;&lt;li &gt;&lt;i class= "Layui-icon" &gt;&amp; #xe63a; </xmp>