note 77973 added to function.preg-replace
| From: | DevilaN at osu1 dot php dot net | Date: | Fri, 21 Sep 2007 23:48:54 +0000 |
| Subject: | note 77973 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130681@lists.php.net to get a copy of this message | ||
Be carefull with substitutions
Following code should remove all single letters near spaces and remove unnecesary duplicated spaces:
<?php
$f = " A B C Defg ";
$f = preg_replace('/\s[^\s]\s/', ' ', $f);
echo trim(preg_replace('/\s\s+/', ' ', $f)) . "\n";
?>
Will result:
B Defg
Why the letter "B" has not been removed?
This is because matched pattern cannot match the same part of text twice, and so it will be as
(space-A-space) is matched, then next is (space-C-space)
Regards
----
Server IP: 212.124.37.9
Probable Submitter: 82.160.181.2
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/77973
Del: integrated -- https://master.php.net/note/delete/77973/integrated
Del: useless -- https://master.php.net/note/delete/77973/useless
Del: bad code -- https://master.php.net/note/delete/77973/bad+code
Del: spam -- https://master.php.net/note/delete/77973/spam
Del: non-english -- https://master.php.net/note/delete/77973/non-english
Del: in docs -- https://master.php.net/note/delete/77973/in+docs
Del: other reasons-- https://master.php.net/note/delete/77973
Reject -- https://master.php.net/note/reject/77973
Search -- https://master.php.net/manage/user-notes.php