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? Quote Link to comment 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... Quote Link to comment 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> Quote Link to comment 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!! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.