PHP Intercept String function (Chinese string) (_php) tutorial

Source: Internet
Author: User
Tags closing tag
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 '

&lt;xmp&gt; '; &amp;lt;br/&amp;gt;echo html_substr ($STR, 5). Php_eol;&amp;lt;br/&amp;gt;echo Html_substr ($STR, 5, 1); &amp;lt;/p&amp;gt;&amp;lt;/blockquo Te&amp;gt;&amp;lt;p&amp;gt;1 2 &amp;lt;/p&amp;gt;&amp;lt;p align= "left" &amp;gt;&amp;lt;span id= "url" itemprop= "url" &amp;gt;http://www.bkjia.com/phpjc/ 632042.html&amp;lt;/span&amp;gt;&amp;lt;span id= "Indexurl" itemprop= "Indexurl" &amp;gt;www.bkjia.com&amp;lt;/span&amp;gt;&amp;lt;span id= " Isoriginal "itemprop=" isoriginal "&amp;gt;true&amp;lt;/span&amp;gt;&amp;lt;span id=" Isbasedonurl "itemprop=" IsBasedOnUrl "&amp;gt;http ://www.bkjia.com/phpjc/632042.html&amp;lt;/span&amp;gt;&amp;lt;span id= "Genre" itemprop= "genre" &amp;gt;TechArticle&amp;lt;/span&amp;gt; &amp;lt;span id= "description" itemprop= "description" &amp;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, ... &amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;li &amp;gt;&amp;lt;i class= "Layui-icon" &amp;gt;&amp;amp; #xe63a; &lt;/xmp&gt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.