Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:41+00:00 2026-06-13T09:49:41+00:00

Users can enter text for a piece of content using WYSIWYG which is placed

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:49:42+00:00Added an answer on June 13, 2026 at 9:49 am

    This should do the trick, if I’m not mistaken:

    preg_match_all('/height(\:|\=)"*\s*([0-9]+[^;"]+);*/i','<tr style="height: 123px; border: none;><tr height="125px"',$matches);
    var_dump($matches[2]);//array('123px','125px');
    

    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:

    function getDeepChildren($node,&$nodeArray)
    {//recursive function to flatten dom
        $current = $node->getElementsByTagName('*');//get all children
        foreach($current as $node)
        {//loop through children
            $nodeArray[] = $node;//add child
            if ($node->hasChildNodes())
            {//if child node has children of its own
                getDeepChildren($node,$nodeArray);//get the children and append to nodeArray
            }
        }
    }//no return value, $nodeArray is passed by reference
    $dom = new DOMDocument();
    $dom->loadHTML($body);
    $nodes = array();
    getDeepChildren($dom,$nodes);//$nodes is passed by reference
    $height = array();
    while($node = array_shift($nodes))
    {//$height[i][0] === height value, $height[i][1] is reference to node
        if ($node->hasAttribute('height'))
        {
            $height[] = array($node->getAttribute('height'),$node);
            continue;//already got what we need, no need for slow preg_match
            //in case of <div height="123px" style="border:1px solid #F00;"> for example...
        }
        if ($node->hasAttribute('style') && preg_match('/height\s*\:\s*([0-9]+\s*[a-z]+)\s*;/i',$node->getAttribute('style'),$match))
        {
            $height[] = array($match[1],$node);
        }
    }
    var_dump($height);//should contain everything you're looking for
    

    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:

    function getDeepChildren($node)
    {
        $nodes = array();
        $current = $node->getElementsByTagName('*');
        foreach($current as $node)
        {
            $nodes[] = $node;
            if ($node->hasChildNodes())
            {
                $nodes = array_merge($nodes,getDeepChildren($node));
            }
        }
        return $nodes;
    }
    //instead of getDeepChildren($dom,$nodes), usage is:
    $nodes = getDeepChildren($dom);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several textboxes where users can enter information into them. This can include
Im building an app which uses Sqlite DB. Users can enter their information into
I have a custom developed CMS where users can enter some content into a
I have a textarea which users can enter some tags. I need to limit
I am writing a little note pad app, where users can enter text notes,
I have a content editable div where users can enter their bio and then
I am trying to implement an interface where users can dynamically enter text and
i have a text field where users can enter a string of 15 numbers
I want to give users a text area where they can enter text. Later
I'm using system.net.mail and have a textbox that users can enter their email address

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.