preg replace - convert ereg_replace to preg_replace in php -
this question has answer here:
- replace ereg_replace preg_replace 4 answers
i have following ereg_replace statement:
ereg_replace ( ".*alternative0=\"[^\"]*\"[ ]{0,10}>", "", $v );
since ereg_replace
deprecated upgrade preg_replace
, want upgrade code first occurrence replaced.
preg_replace ("/.*alternative0=\".*?\".*>/", "", $v,1 );
but seems work partially.
the major problem when have whitespace between "
, >
preg not work
here example strings want to change:
<tag type="head" alternative0="not head">{!head!}</tag> <tag type="tail" alternative0="tail>{!not tail!}</tag>
but may be:
<tag type="head" alternative0="not head">{! xxxx !}</tag>
or even:
<tag type="header" alternative0="not head " > {! blah bla !}</tag>
you have more efficient pattern this:
preg_replace ('/.*alternative0="[^"]++"[^>]*+>/', "", $v, 1);
you can't use like: .*>
because dot match characters , quantifier * greedy .*
match final > , >
pattern never matched or not matched @ offset.
Comments
Post a Comment