preg replace - convert ereg_replace to preg_replace in php -


this question has answer here:

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

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -