tryingtolearn Posted February 20, 2007 Share Posted February 20, 2007 Hello, I started this post over on the help board before I realized this board was even here. I hope someone can help with this or even let me know if its possible. I am totally confused over this preg_replace Basically I am not sure what function I should be using to accomplish something. What I need to do is convert everything between multiple html comment tags so they do not get stripped when using strip tags.( might not be an img tag in there either - so what ever is between the two comment tags I need in a format that wont get touched by the strip_tags) So if I start with this Bunch of html code <!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK --> <img src="graphics/pic2.gif" width="40" height="40" alt="mypic2" border="0"> <!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK --> Bunch of html code It will change to something like this Bunch of html code <!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK --> <img src="graphics/pic2.gif" width="40" height="40" alt="mypic2" border="0"> <!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK --> Bunch of html code So I can run it through strip tags and end up with this back on the html page. Bunch of text <!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK --> <!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK --> Bunch of text There can be any # of the STARTMARK and ENDMARK tags from 0-100 it will just depend on the page. I have everything but converting only those marked sections Can this be done? Im this far but am lost when it comes to replacing everything in the middle of the tags. $search = array ("'<!-- STARTMARK --[^>]*?>.*?<!-- ENDMARK -->'si"); $replace = array ("<!-- STARTMARK -->.*<!-- ENDMARK -->"); $new = preg_replace ($search, $replace, $source); Any ideas??? Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/ Share on other sites More sharing options...
effigy Posted February 20, 2007 Share Posted February 20, 2007 Use preg_replace_callback in order to pass the captured text through htmlentities. Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/#findComment-189219 Share on other sites More sharing options...
tryingtolearn Posted February 20, 2007 Author Share Posted February 20, 2007 OK I can try that but I cant capture the text inbetween the tags? I guess thats where the confusion begins! Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/#findComment-189336 Share on other sites More sharing options...
tryingtolearn Posted February 20, 2007 Author Share Posted February 20, 2007 Well I dont know how but I tried this and it works but there are two issues 1. It seems like a crude way of doing it (Compared to what effigy was saying with the callback) 2. It only seems to work with an image tag I had to replace the <img to get the contents back - an imag in this example. $search2 = array ("'<!-- STARTMARK -->[<]([a-zA-Z]|[0-9])*'"); $replace2 = array ("IfR!-- STARTMARK --IfR XXX "); $strip2 = preg_replace ($search2, $replace2, $strip); $newcode = strip_tags($strip2, '<p>,<br>,<H1>'); $search3 = array ("'IfR!-- STARTMAK --IfR'","'XXX'"); $replace3 = array ("<!-- STARTMARK -->","<img"); $strip3 = preg_replace ($search3, $replace3, $newcode); This is very confusing. Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/#findComment-189346 Share on other sites More sharing options...
effigy Posted February 20, 2007 Share Posted February 20, 2007 Actually, the /e modifier would be easier. ( )'s are used to capture data, which can be accessed via backreferences. See the links in my signature for more info. <pre> <?php $data = <<<DATA Bunch of html code <!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK --> <img src="graphics/pic2.gif" width="40" height="40" alt="mypic2" border="0"> <!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK --> Bunch of html code DATA; echo $data, '<hr>'; $data = preg_replace('/<!-- STARTMARK -->(.+?)<!-- ENDMARK -->/se', 'htmlentities("\1")', $data); echo $data; ?> </pre> Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/#findComment-189530 Share on other sites More sharing options...
tryingtolearn Posted February 20, 2007 Author Share Posted February 20, 2007 Thanks effigy, I will look at in depth when I get back this evening. I spent a loooong time at those links last night. They are helpful it just takes me a really long time to get this straight. Wow - Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/#findComment-189570 Share on other sites More sharing options...
tryingtolearn Posted February 20, 2007 Author Share Posted February 20, 2007 Worked Perfect! THanks I appreciate your time (and the links) Link to comment https://forums.phpfreaks.com/topic/39260-solved-not-sure-if-preg_replace-is-what-i-need/#findComment-189746 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.