php - Remove complete HTML tag when characters are found -


a string contains html tag word + suffix (in case ...rem)

example:

<b>sometext...rem</b> <u>sometext...rem</u> <strong>sometext...rem</strong> <a href="/">sometext...rem</a> <div>sometext...rem</div> 

when word inside html tag contain

...rem 

the complete html tag + word should removed.

i can rename "...rem". marker.

is possible?

i suggest using html parser this. however, since question asks regular expression, use following , replace matches in callback.

/(?s)<(\w+)[^>]*>(.*?)<\/\1>/ 

explanation:

  • (?s) - s flag . character matches newlines characters.
  • <(\w+)[^>]*> - match opening html tag , capture element name
  • (.*?) - second capturing group match contents of html tag
  • <\/\1> - match closing html tag using reference based on first capturing group (which tag name).

then use function preg_replace_callback in order replace match empty sting if second capturing group contains substring ...rem. otherwise, nothing replacing match itself.

live example here

preg_replace_callback('/(?s)<(\w+)[^>]*>(.*?)<\/\1>/', function ($m) {   return strpos($m[2], '...rem') !== false ? '' : $m[0]; }, $string); 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -