简体   繁体   中英

Replace Strings Which Not Wrapped in a HTML Element with Regex in PHP

I am trying to overcome this conversion with Regex:

abc <a href="#abc">abc</a> abc

To:

lorem <a href="#abc">abc</a> lorem

My regex is:

preg_replace('/[^<="\']*.*(abc).*[^>="\']*/i','lorem',$text)

I tried many combinations, not help to convert.

What Regex does handle that?

Try this:

(.*)(<a(.*?)\/a>)(.*)


group 1: abc
group :2 <a href="#abc">abc</a>
group 3: abc

I don't know much about regular expressions but you can try a php function

str_replace(find,replace,string,count)

Eg:

echo str_replace("world","Peter","Hello world!");

the example above will replace "world" with peter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM