0

i have the following ereg_replace statement:

ereg_replace ( ".*alternative0=\"[^\"]*\"[ ]{0,10}>", "", $v );

since ereg_replace is deprecated I would like to upgrade it to preg_replace and I also want to upgrade my code so only the first occurrence will be replaced.

preg_replace ("/.*alternative0=\".*?\".*>/", "", $v,1 );

but it seems to work partially.

the major problem is that when I have whitespace between the " and > my preg does not work

here are some example strings i want to to change:

<tag type="head" alternative0="not head">{!head!}</tag>
<tag type="tail" alternative0="tail>{!not tail!}</tag>

but it also may be:

<tag type="head" alternative0="not head">{!
xxxx   !}</tag>

or even:

<tag type="header" alternative0="not head "    > {!  blah bla !}</tag>
sd1sd1
  • 1,028
  • 3
  • 15
  • 28
  • Tip: First convert. Ensure it works as before. Then ask a second question about your (new) regex problem with the count of matching. – hakre May 04 '13 at 21:24
  • Maybe provide some example strings that you want to match your pattern, since you're converting and slightly changing the behavior of your pattern from `"[^\"]*"[ ]{0,10}` to `".*?".*` – Mark Fox May 04 '13 at 21:25
  • maybe someone can explain to me what is the preg equivalent to "[ ]{0,10}" since that seems to be the only issue in my pattern. iv'e edieted the post and added some examples – sd1sd1 May 05 '13 at 04:13

1 Answers1

0

You will have a more efficient pattern with this:

preg_replace ('/.*alternative0="[^"]++"[^>]*+>/', "", $v, 1);

You can't use something like: .*> because the dot match all characters and the quantifier * is greedy thus the .* match the final > too and the > from the pattern is never matched or not matched at the good offset.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125