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?

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.