user2846346
user2846346

Reputation: 1

Using preg_replace and regex in php

I have this string in PHP

$content="<a some_text> {pr_start} some_text</a> <a other_text>{pr_stop}</a>
          <a some_text> {pr_start} </a> <a some_text> {pr_start} </a>";

I want to replace all the occurrences of the substring

"<a some_text> {pr_start}  some_text</a>"

with text "START" and leave the rest as it is!

The result expected is:

"START <a other_text>{pr_stop}</a> START START"

I used

preg_replace('#<a(.*)({pr_start})(.*)</a>#',"START",$content);

Any idea? Thanks!

Upvotes: 0

Views: 42

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You need to add an ungreedy flag U in your pattern:

preg_replace('#<a(.*)({pr_start})(.*)</a>#U',"START",$content);

Upvotes: 2

Related Questions