regex - Replace one character with another using PHP function -
i need use custom php function process data,
austin metro>central austin|austin metro>georgetown|austin metro>lake travis | westlake|austin metro>leander | cedar park|austin metro>round rock | pflugerville | hutto|austin metro>southwest austin
and convert reads this:
austin metro>central austin#austin metro>georgetown#austin metro>lake travis | westlake#austin metro>leander | cedar park#austin metro>round rock | pflugerville | hutto#austin metro>southwest austin
currently using following replacing character "|" between "leander | cedar park". there way replace ones no space before or after?
preg_replace("/|/", "#", {categories[1]} );
any suggestions? thanks!
what you're looking called ahead/behind assertion in pcre. want negative behind , negative ahead space surrounding pipe. should note character |
has special meaning in pcre need escape literal.
preg_replace('/(?<! )\|(?! )/', '#', $str);
the (?<! )
negative look-behind. says match character |
, if not proceeded space. (?! )
negative ahead. says match character |
if not followed space.
Comments
Post a Comment