note 77973 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Fri, 21 Sep 2007 23:56:56 +0000 |
| Subject: | note 77973 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-130682@lists.php.net to get a copy of this message | ||
Note Submitter: DevilaN
----
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