0

I have an html string

$html_string = '<div style="font-family:comic sans ms,cursive;">
<div style="font-size:200%;">Some Text </div></div>';

I have tried

$dom = new DOMDocument;
$dom->loadHTML($html_string);
$divs = $dom->getElementsByTagName('div');

for($i=0;$i<$divs->length;$i++) {
$attrib = $divs->item($i)->getAttribute("style");
echo $attrib;
echo '<br />';
}

it gives the following output

font-family:comic sans ms,cursive
font-size:200%;

I need

font-family
font-size

How can I get only these keys not the values they have?

1
  • You do not want the keys instead of the values, because these would be "style" in both cases. If I guess right, you want the property names from the CSS string that the values are. How do you wish to handle cases where multiple properties are set, e.g. <div style="color:red;font-weight:bolder">text</div>? Commented Mar 28, 2013 at 14:48

3 Answers 3

1

you can use regexps to do that. Something like this:

$style = 'font-family:comic sans ms,cursive;font-size:15em';
preg_match_all('/(?<names>[a-z\-]+):(?<params>[^;]+)[; ]*/', $style, $matches);

var_dump($matches['names']);
var_dump($matches['params']);

result:

array
  0 => string 'font-family' (length=11)
  1 => string 'font-size' (length=9)

array
  0 => string 'comic sans ms,cursive' (length=21)
  1 => string '15em' (length=4)

this even works with with more than one css parameter

Sign up to request clarification or add additional context in comments.

1 Comment

that's pretty more conformable.
1

Use a CSS parser!

All the answers with explode and regular expressions are inherently wrong. It is CSS source-code you're trying to analyze. Simple text-manipulation will never do that correctly. E.g. background-image:url('http://my.server.com/page?a=1;b=2'); list-style-image:url('http://my2.server.com/page/a=1;b=2') is perfectly valid, contains the two properties background-image and list-style-image and most text-processing will fail either because there semicolons or 4 colons in the middle of the text (both would be mistaken by poor solutions to indicate 4 properties).

Generally, never try fiddling with text-manipulation tools in source code; not for CSS, nor HTML, nor any sourcecode else. Languages are by design more complicated than that. This is what parsers are meant to accomplish, and it is the same reason why they are BIG -- or at least more complicated than strpos()...

1 Comment

Yes it is, as is the example code. The problem's that what you refer to as "y" also may contain colons & semicolons that render your suggestion unusable. You would first explode it by semicolons yielding 4 parts although there are 2 legit parts. Possibly more complicated examples could be constructed to prove most of the basic explode-based solutions wrong. My point was not to show an example that refutes all simple text-processing attempts, but to call to your attention not to even try thinking about the quirks. Leave that to specialized software, a parser; or your code will eventually break.
0

Use explode on your current output and then go on using the first element you received from explode

1 Comment

This suggests very low quality ... think of all the cases when it does not work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.