Users can enter text for a piece of content using WYSIWYG which is placed into the variable $body. This may include multiple instances of style="[maybe stuff] height:xpx [maybe stuff]" or height="xpx".
I need to get all of the height values that exist (the numbers only) so that I can add them together.
Note there may be other integer values within the string so it can’t just grab all integers.
If the solution uses regex, I have never been able to understand it and I understand there are security issues with regex, so ideally I’m looking for a safe solution!
I’m certain this must be quite simple but I’m struggling!
This should do the trick, if I’m not mistaken:
But since you’re going to let this regex loose on HTML (if I’m not mistaken), I’d look at ways to parse the DOM and use the DOMElement’s methods to get what I want. It’s a far more robust take on the problem.
As requested by OP:
For a more OO approach, I suggest looking at a couple of recursive domnode iterator classes.
Passing arrays by reference is discouraged, but it’s the easiest way to get what you need here. An alternative version would be: