PHP String Replacement Not Working Properly -
here php code,
$string = 'https://www.mydomain.lk/'; $wordlist = array("http://", "www.", "https://", "/"); foreach ($wordlist &$word) { $word = '/\b' . preg_quote($word, '/') . '\b/'; } echo $string2 = preg_replace($wordlist, '', $string);
i want remove last "/" $string. add "/" $wordlist array, not working.
can me fix this. thanks.
you want replace /
@ end of string, need $
, /$
, preg_quote
end escaping $
.
the best way remove trailing /
using rtrim, sudhir suggested. alternatively remove preg_quote
loop , use regular expressions in $wordlist
:
$string = 'https://www.mydomain.lk/'; $wordlist = array("#https?://#", "#www\.#", "#/$#"); echo $string2 = preg_replace($wordlist, '', $string);
Comments
Post a Comment