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:[email protected]'>[email protected]</a>");[/code] returns [email protected]how does one remove the whole thing? from <a to </a? Link to comment https://forums.phpfreaks.com/topic/20873-removing-a-tag-and-everything-inbetween/ 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:[email protected]'>[email protected]</a>XYZ";$pattern = '/(\<a)(.+?)(\<\/a\>)/i';echo preg_replace($pattern,"", $text);[/code]Ronald 8) Link to comment https://forums.phpfreaks.com/topic/20873-removing-a-tag-and-everything-inbetween/#findComment-92519 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? Link to comment https://forums.phpfreaks.com/topic/20873-removing-a-tag-and-everything-inbetween/#findComment-92600 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. Link to comment https://forums.phpfreaks.com/topic/20873-removing-a-tag-and-everything-inbetween/#findComment-92601 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? Link to comment https://forums.phpfreaks.com/topic/20873-removing-a-tag-and-everything-inbetween/#findComment-92613 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! Link to comment https://forums.phpfreaks.com/topic/20873-removing-a-tag-and-everything-inbetween/#findComment-92658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.