PHP Intercept String function (Chinese string) (1/2)

Source: Internet
Author: User
Tags memory usage strlen
The code is as follows Copy Code

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 the start tag, then the stack, if it is corresponding to the end tag is out of the stack
if (Preg_match ('/< ([^/]+)? >/i ', $tag, $r)) {
echo ' into stack: ', Htmlspecialchars ($r [1]), ' <br/> ';
Array_push ($tagstack, $r [1]);
}
ElseIf (Preg_match ('/'. $tagstack [Count ($tagstack)-1]. ' /', $tag)) {
Echo ' Out stack: ', Htmlspecialchars ($tagstack [Count ($tagstack)-1]), ' <br/> ';
Array_pop ($tagstack);
}

$tag = ';
Continue
}

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


The final result of Echo '

Stack is empty 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 = "A1<body>b2<p>c3<em>d4</em>e5</p>f6</body>g7h8";

echo ' processing results are: <br/>

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

echo "Algorithm run Time (microtime):" (Microtime (True)-$sttime), ' <br/> ';

Method Two

/

The code is as follows Copy Code

**
* Html_substr of function name
* Function intercepts the specified length of string from the HTML string, and the HTML tag is not counted
* Parameter
* $str the string to be intercepted
* $len the length to be intercepted
* $mode mismatched mark processing mode 0 Delete (default), 1 padded
* Return to the intercepted string
* Description
* Does not consider multibyte characters, only bytes do count units
* No separate tags are 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 (' < ', ' </', $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);

Home 1 2 last page

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.