php - Replace HTML entities between divs and only between divs -


given following string:

asd &nbsp; <div> def &nbsp; foo &nbsp; </div> ghi &nbsp; <div> moo &nbsp; </div> 

i want remove of &nbsp;'s within <div>s, resulting in:

asd &nbsp; <div> def  foo  </div> ghi &nbsp; <div> moo  </div> 

i can use standard php stuff, i'm not sure how approach problem. couldn't figure out how keep contents inside <div>s while removing &nbsp;

the reason why need because wordpress's content filter adds &nbsp; under strange situations. can't remove &nbsp; because might've been entered user, need remove of them within element that's having display problems caused them

the following works in case:

$str = "asd &nbsp; <div> def &nbsp; </div> ghi &nbsp; <div> moo &nbsp; </div>"; $res = preg_replace("%<div>(.*?)&nbsp;(.*?)</div>%", "<div>$1$2</div>", $str); 

but beware of facts:

  • it won't work if divs have attributes;
  • it won't work expected if divs nested;
  • it applies replacement of &nbsp; 1 time, multiple &nbsp;s inside divs untouched.

so abovementioned replacement not solution @ all. it's way better first find div tags (xml) parser function , replace &nbsp;s.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -