php - Ignore <script>-Tag while replace Keywords with Links -


this question has answer here:

i want replace keywords in text link. created preg_replace:

$include = preg_replace('/(?!(?:[^<]+>|[^>]+<\/(span)><\/(a|option|h3)>|[^>]+<\/(a|option|h3|h4|h5|textarea|input|script)>))\b('.$key->key_word.')\b/is', '<a href="/'.$lang.'/'.$key->key_area.'/'.$key->key_page.'.html" title="'.$key->key_title.'" class="keylink">\\4</a>',$include,$limit,$count); 

now there problem regexp works within javascript (...). how can change solve problem?

thank help!

soapbox

we craft regex match specific case, given html parsing , use case hints number of tags in there, you'd best off using dom or using product html agility (free)

however

consider following powershell example of universal regex. in example iterating on each matching text outside script tags. here apply string replacement each matched group. should noted break if inside script use string looks open or closed <script> tags.

(?:^|</script[^>]*>)(.*?)(?=<script\s|$)

enter image description here

example

$regex = '(?:^|</script[^>]*>)(.*?)(?=<script\s|$)' $string = 'i wanna match string contains<script src=value> > that; bla ; bla < this</script> match "bla" '  write-host start  write-host $string write-host write-host found $matches = @() ([regex]"(?i)$regex").matches($string) | foreach {     write-host "value @ $($_.groups[1].index) = '$($_.groups[1].value)'"     } # next match 

yields

start wanna match string contains<script src=value> > that; bla ; bla < this</script> match "bla"   found value @ 0 = 'i wanna match string contains' value @ 89 = ' match "bla" ' 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -