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

  • Home
  • SEARCH
  • 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 6796233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:26:33+00:00 2026-05-26T18:26:33+00:00

How can I get the width and height of each element’s attribute? For instance,

  • 0

How can I get the width and height of each element’s attribute?

For instance,

$dom = new DOMDocument;
$dom->loadHTML('<div class="item" style="width:295px; height:210px; border:1px solid #000;"></div><div></div>');

foreach( $dom->getElementsByTagName('div') as $node ) 
{
    $style = $node->getAttribute( 'style' );

    var_dump($style);
}

result,

string 'width:295px; height:210px; border:1px solid #000;' (length=49)
string '' (length=0)

but these are what I am after,

  1. select the div that has the classname of item only.
  2. get 295 (width) and 210 (height) only.

Is it possible with DOM? Or XPATH?

EDIT:

I seem to manage to select the div with the classname now,

$dom = new DOMDocument;
$dom->loadHTML('<div class="item" style="width:295px; height:210px; border:1px solid #000;"></div><div></div>');

$xpath = new DOMXpath($dom); 

foreach ($xpath->query('//*[@class="item"]') as $node) {

    $style = $node->getAttribute( 'style' );

    var_dump($style);
}

Now this is what I am after only,

get 295 (width) and 210 (height).

  • 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-05-26T18:26:34+00:00Added an answer on May 26, 2026 at 6:26 pm

    If you don’t want to use a regex, you can use simple string functions to extract what you need. Here is an example, it most likely can be improved upon.

    $width = 'width:'; $height = 'height:';
    // Adding whitespace will not affect the result
    $str = '    width:   295 px; height:   210 px; border:1px solid #000;';
    
    $width_str = strstr( $str, $width);
    echo 'Width: "' . trim( substr( $width_str, strlen( $width), stripos( $width_str, 'px;') - strlen( $width))) . '"';
    
    echo "\n";
    
    $height_str = strstr( $str, $height);
    echo 'Height: "' . trim( substr( $height_str, strlen( $height), stripos( $height_str, 'px;') - strlen( $height))) . '"';
    

    Of course, you can replace the $width and $height variables with their string literals, and remove the calls to strlen() as they would be constant integers.

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get the width of my LinkButton object ? myLinkButton = new
As in the subject, how can one get the total width of an element,
I have a bunch of elements like the following: <div class=droppableP id=s-NSW style=width:78px; height:63px;
I have text with font in org.faceless.pdf.PDFFont. How can I get width of it
I'm trying to get the height, width, and placement of a section of HTML
I have a gridview control that can get loaded with multiple pages worth of
I have a file that I can get to with the UNC \mypath\myfile. I
Can get all triples with value null in specific field? All people with date_of_birth
Can anyone get me with the regular expression to strip multiline comments and single
Can I get paletted textures with RGB palette and 8-bit alpha channel in OpenGL

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.