php interception string function (Chinese string)

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array_pop closed content echo function html htmlspecialchars it is

PHP tutorial interception string function (Chinese string)
This is a php interception string function Oh, this is a support Chinese string oh, it can intercept html and Chinese and Western mixed content, and the html tag is not included in the character interception, if the html tag is not Closed, the program will automatically filter excess labels.
* /

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, if it is the corresponding end tag is popped
if (preg_match ('/ <([^ /] +>?> / i', $ tag, $ r)) {
echo 'into the stack:', htmlspecialchars ($ r [1]), '<br />';
array_push ($ tagstack, $ r [1]);
}
elseif (preg_match ('/'.$tagstack[count($tagstack)-1].'/', $ tag)) {
echo 'pop:', htmlspecialchars ($ tagstack [count ($ tagstack) -1]), '<br />';
array_pop ($ tagstack);
}

$ tag = '';
continue
}

$ charcnt ++;
$ resultstr. = $ str [$ i];
}


echo '<hr size = 1> The final result is:';

// The stack is empty and returned directly
if (empty ($ tagstack)) {
return $ resultstr;
}
// otherwise remove the start tag without an 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 = "a1 <body> b2 <p> c3 <em> d4 </ em> e5 </ p> f6 </ body> g7h8";

echo 'results in: <hr size = 1>', htmlspecialchars (mysubstr ($ str, 18)), '<br />';

echo "Memory Usage:", (memory_get_usage () - $ stmem), '<br />';

echo "microtime:", (microtime (true) - $ sttime), ';'
//Method Two

/

**
* Function name html_substr
* Function from the html string interception of the specified length of the string, html tags are not counted
* Parameters
* $ str string to be intercepted
* $ len The length to be intercepted
* $ mode does not match the treatment of the flag 0 Delete (default), 1 filled
* Return the truncated string
* Description
* Not considered multi-byte characters, only the byte count units
* Does not consider the existence of the mark alone
** /
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 ('<', '</', $ v);
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 = "123 <em> abc </ em> 456 <em> def </ em> 789";

echo '<xmp>';
echo html_substr ($ str, 5). php_eol;
echo html_substr ($ str, 5, 1);
$ str = "a1 <body> b2c3 <p> <em> d4 </ em> e </ p> 5f6 </ body> g7h8";
$ gn = 7;
$ i = $ j = $ k = 0;
while (($ c = $ str [$ i ++]) && $ j <$ gn)
{
if ($ c == '<')
{
$ tag = 1;
}
elseif ($ c == '>')
{
if (trim ($ tg, '/') == 'em')
{
$ tgs [$ j-1] = '<'. $ tg. '>';
}
else
{
if ($ tgs [$ j-1]) $ ogs [$ j-1] = '1 |'. '<'. $ tg. '>';
else $ ogs [$ j-1] = '0 |'. '<'. $ tg. '>';
}
$ tag = 0;
$ tg = '';
}
elseif ($ tag == 1)
{
$ tg. = $ c;
}
else
{
$ tmp [] = $ c;
$ j ++;
}
}
$ ts = count ($ tgs);
if ($ ts% 2) array_pop ($ tgs);
foreach ($ tmp as $ k => $ v)
{
$ ba = explode ('|', $ ogs [$ k], 2);
if ($ tgs [$ k] && $ ogs [$ k])
{
if ($ ba [0])
{
$ s. = $ v. $ tgs [$ k]. $ ba [1];
}
else $ s. = $ v. $ ba [1]. $ tgs [$ k];
}
else $ s. = $ v. $ tgs [$ k]. $ ba [1];
}
echo htmlspecialchars ($ s);

Related Article

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.