Tripped over a subtle issue in preg_replace!
Eventually got the right answers from the readers at
alt.comp.lang.php!
preg_replace assumes a /g - replace all occurences.
So, when you need a replacement to force a single /
character at end of string, i.e, replace 0 or more /
characters with single /, here's what you need:
$t = preg_replace('!/*$!', '/', $s, 1);
Note the all important 1 (limit) at the end. Default is -1,
and it seems to force a non-greedy match for /*$ , so if $s
== aa//, the output is still a// with limit -1.
But with 1 as the limit, all values of $s such as aa or aa/
or aa// or aa//// all end up like aa/, just as needed.
[Not sure why a//// ends up as a// with limit -1...]
So, final story, to make *$ be greedy, use the limit of 1.
----
Server IP: 216.194.113.175
Probable Submitter: 216.15.114.45
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master
.php.net/note/edit/70833
Del: integrated -- h
ttps://master.php.net/note/delete/70833/integrated
Del: useless -- http
s://master.php.net/note/delete/70833/useless
Del: bad code -- htt
ps://master.php.net/note/delete/70833/bad+code
Del: spam -- https:/
/master.php.net/note/delete/70833/spam
Del: non-english --
https://master.php.net/note/delete/70833/non-english
Del: in docs -- http
s://master.php.net/note/delete/70833/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/70833
Reject -- https://mast
er.php.net/note/reject/70833
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|