php interception string function (Chinese string) This is a PHP string interception function. It supports Chinese strings. It can intercept mixed content such as HTML and Chinese and Western texts, and the HTML tag is not included in the character interception. If the HTML tag does not Closed, the program will automatically filter out redundant tags.
php tutorial interception string function (Chinese string)
This is a PHP string interception function. It supports Chinese strings. It can intercept mixed content such as HTML and Chinese and Western texts, and the HTML tag is not included in the character interception. If the HTML tag does not Closed, the program will automatically filter out redundant 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 = '';
$tag .= $str[$j];
}
$tagcnt++;
$length++;
$tag .= '>';
// If it is a start tag, push it onto the stack, if it is a corresponding end tag, pop it out of the stack
If( preg_match('/<([^/]+)?>/i', $tag, $r) ){
Echo 'into the stack:', htmlspecialchars ($ r [1]), '& lt; br /& gt;';
array_push($tagstack, $r[1]);
}
elseif( preg_match( '/'.$tagstack[count($tagstack)-1].'/', $tag ) ){
Echo 'out of the stack:', htmlspecialchars ($ tagstack [count ($ tagstack) -1]), '& lt; br /& gt;';
array_pop( $tagstack);
}
continue;
$charcnt++;
}
$resultstr .= $str[$i];
}
echo '
The final result is:';
//The stack is empty and returns directlyIf(empty($tagstack)){
$tag = array_pop($tagstack);
return $resultstr;
}
//Otherwise remove the start tag without end tag
else{
while(!empty($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
b2c3d4e5
f6g7h8";echo 'The processing result is:
',htmlspecialchars( mysubstr( $str, 18 ) ),'
';echo "Memory usage:",(memory_get_usage()-$stmem),'
';echo "Algorithm running time (microtime):",(microtime(true)-$sttime),'
';
//Method 2
/
**
* Function name html_substr
* Function: intercept a string of specified length from the html string, html tags are not included
* Parameter
* $str The string to be intercepted
* $len The length to be intercepted
* $mode processing method of unmatched tags 0 deletes (default), 1 completes
* Return the intercepted string
* Description
* Multi-byte characters are not considered, only bytes are used as the counting unit
* Marks that can exist alone 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('<', '', $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 = "123abc456def789";echo '
';
echo html_substr($str, 5) .php_eol;
echo html_substr($str, 5, 1);
1 2