note 113235 added to ref.mbstring
| From: | DanielRhodes | Date: | Mon, 16 Sep 2013 12:11:41 +0000 |
| Subject: | note 113235 added to ref.mbstring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196269@lists.php.net to get a copy of this message | ||
Here's a cheap and cheeky function to remove leading and trailing *punctuation* (or more
specifically "non-word characters") from a UTF-8 string in whatever language. (At least it
works well enough for Japanese and English.)
/**
* Trim singlebyte and multibyte punctuation from the start and end of a string
*
* @author Daniel Rhodes
* @note we want the first non-word grabbing to be greedy but then
* @note we want the dot-star grabbing (before the last non-word grabbing)
* @note to be ungreedy
*
* @param string $string input string in UTF-8
* @return string as $string but with leading and trailing punctuation removed
*/
function mb_punctuation_trim($string)
{
preg_match('/^[^\w]{0,}(.*?)[^\w]{0,}$/iu', $string, $matches);
//case-'i'nsensitive and 'u'ngreedy
if(count($matches) < 2)
{
//some strange error so just return the original input
return $string;
}
return $matches[1];
}
Hope you like it!
----
Server IP: 69.147.83.199
Probable Submitter: 86.28.217.184
----
Manual Page -- http://php.net/manual/en/ref.mbstring.php
Edit -- https://master.php.net/note/edit/113235
Del: integrated -- https://master.php.net/note/delete/113235/integrated
Del: useless -- https://master.php.net/note/delete/113235/useless
Del: bad code -- https://master.php.net/note/delete/113235/bad+code
Del: spam -- https://master.php.net/note/delete/113235/spam
Del: non-english -- https://master.php.net/note/delete/113235/non-english
Del: in docs -- https://master.php.net/note/delete/113235/in+docs
Del: other reasons-- https://master.php.net/note/delete/113235
Reject -- https://master.php.net/note/reject/113235
Search -- https://master.php.net/manage/user-notes.php