tibberous Posted August 6, 2007 Share Posted August 6, 2007 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 https://forums.phpfreaks.com/topic/63523-solved-matching-the-style-section-of-an-html-page/ Share on other sites More sharing options...
tibberous Posted August 6, 2007 Author Share Posted August 6, 2007 I found the code to do it in javascript, but it looks like javascript regex is different than PHP regex... Link to comment https://forums.phpfreaks.com/topic/63523-solved-matching-the-style-section-of-an-html-page/#findComment-316579 Share on other sites More sharing options...
effigy Posted August 6, 2007 Share Posted August 6, 2007 <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 https://forums.phpfreaks.com/topic/63523-solved-matching-the-style-section-of-an-html-page/#findComment-316728 Share on other sites More sharing options...
tibberous Posted August 6, 2007 Author Share Posted August 6, 2007 Wow! Thanks! I didn't know [^<] was how you specified everything but <. I have no idea what s at the end does, or why this works, but at this point I'm just trying to get something to show the guy I'm working for. Thanks a ton!! Link to comment https://forums.phpfreaks.com/topic/63523-solved-matching-the-style-section-of-an-html-page/#findComment-316909 Share on other sites More sharing options...
effigy Posted August 7, 2007 Share Posted August 7, 2007 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 https://forums.phpfreaks.com/topic/63523-solved-matching-the-style-section-of-an-html-page/#findComment-317529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.