Jump to content

[SOLVED] Matching the style section of an html page


tibberous

Recommended Posts

I am trying to match a pages style section, basically everything from <style to </style>.

 

I am not sure where I am having trouble, but I remember regex being a lot different than they are. I think I might have learned them in Perl or Javascript or Java or something.

 

Anyway, I get to here.

 

/<\w*style/i

 

And that finds <style or < style or however the user has it.

 

Now, I want to have it match every character up until (<\w*/style\w*>).

 

Does anyone know how to do that?

<pre>
<?php
$html = <<<HTML
	<html>
		<head>
			<title>Test</title>
			<style>* { color: green; }</style>
		</head>
		<body>
			Body.
		</body>
	</html>
HTML;
$pieces = preg_split('#(<style[^>]*>.*?</style>)#s', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as &$piece) {
	$piece = htmlspecialchars($piece);
}
print_r($pieces);

?>
</pre>

I didn't know [^<] was how you specified everything but <.

I have no idea what s at the end does

 

When a ^ is the first character in a character class, it negates the whole set. The "s"--commonly written as /s since forward slashes are the usual delimiters--is a modifier that allows the . to match any character, not just any character except a new line.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.