michaellunsford Posted September 15, 2006 Share Posted September 15, 2006 I'm looking to strip the <style> tag, which includes about ten lines between the opening and closing of the tag.So, a simple example: [code=php:0]striptags("<a href='mailto:somebody@example.com'>somebody@example.com</a>");[/code] returns somebody@example.comhow does one remove the whole thing? from <a to </a? Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted September 15, 2006 Share Posted September 15, 2006 Try this one:[code]$text = "ABC<a href='mailto:somebody@example.com'>somebody@example.com</a>XYZ";$pattern = '/(\<a)(.+?)(\<\/a\>)/i';echo preg_replace($pattern,"", $text);[/code]Ronald 8) Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted September 15, 2006 Author Share Posted September 15, 2006 that's what I'm looking for, but I can't seem to get it to work with the < script and < /script > tags. php.net talks about using a patern, but there isn't a patern reference. Where can I learn how to read your patern and make my own? Quote Link to comment Share on other sites More sharing options...
markbett Posted September 15, 2006 Share Posted September 15, 2006 look in the PHP manual:Example 2. Using indexed arrays with preg_replace()<?php$string = 'The quick brown fox jumped over the lazy dog.';$patterns[0] = '/quick/';$patterns[1] = '/brown/';$patterns[2] = '/fox/';$replacements[2] = 'bear';$replacements[1] = 'black';$replacements[0] = 'slow';echo preg_replace($patterns, $replacements, $string);?>The above example will output:The bear black slow jumped over the lazy dog.By ksorting patterns and replacements, we should get what we wanted.<?phpksort($patterns);ksort($replacements);echo preg_replace($patterns, $replacements, $string);?>The above example will output:The slow black bear jumped over the lazy dog. Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted September 15, 2006 Author Share Posted September 15, 2006 Ahh, but the intricacies of replacing a tag and everything in-between is considerably more complex -- thus the question.so, more specifically: when preg_replace sees this as a patern '/(\<a)(.+?)(\<\/a\>)/i' what is it understanding? And, why wouldn't replacing the instances of the "a" with "style" not work? Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted September 15, 2006 Author Share Posted September 15, 2006 ahh, found a regexp website. apparently, since the style spans several lines, and dot doesn't interpret newlines by default, I either had to add an "s" pattern modifyer, or use [/s/S]+ instead of .+all is well. Thanks for the help! 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.