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??? Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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> Quote Link to comment 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. Quote Link to comment 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) 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.