note 52893 added to function.substr
| From: | fanfatal at fanfatal dot pl | Date: | Tue, 17 May 2005 06:45:49 +0000 |
| Subject: | note 52893 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89515@lists.php.net to get a copy of this message | ||
Hmm ... this is a script I wrote, whitch is very similar to substr, but it isn't takes html and
bbcode for counting and it takes portion of string and show avoided (html & bbcode) tags too ;]
Specially usefull for show part of serach result included html and bbcode tags
<?php
/**
* string csubstr ( string string, int start [, int length] )
*
* @author FanFataL
* @param string string
* @param int start
* @param [int length]
* @return string
*/
function csubstr($string, $start, $length=false) {
$pattern = '/(\[\w+[^\]]*?\]|\[\/\w+\]|<\w+[^>]*?>|<\/\w+>)/i';
$clean = preg_replace($pattern, chr(1), $string);
if(!$length)
$str = substr($clean, $start);
else {
$str = substr($clean, $start, $length);
$str = substr($clean, $start, $length + substr_count($str, chr(1)));
}
$pattern = str_replace(chr(1),'(.*?)',preg_quote($clean));
if(preg_match('/'.$pattern.'/is', $string, $matched))
return $matched[0];
return $string;
}
?>
Using this is similar to simple substr.
Greatings ;]
...
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52893
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52893&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52893&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52893&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52893&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52893&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52893&report=yes
Search -- http://master.php.net/manage/user-notes.php