adrianTNT Posted October 9, 2009 Share Posted October 9, 2009 Hello, I have this text: $my_text= '<pre class="brush: plain"> <div class="header"> <a href="#">home</a> <a href="#">products</a> <a href="#">services</a> </div> </pre>'; I need to get all that content inside the "<pre>..</pre>" tag and convert it to htmlentities. The "class" is different in more situations so I need to match: <pre [anything]>[GIVE ME THIS]</pre> Does that make sense? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/ Share on other sites More sharing options...
corbin Posted October 9, 2009 Share Posted October 9, 2009 ~<pre[^>]*>(.*?)</pre>~ Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934053 Share on other sites More sharing options...
adrianTNT Posted October 9, 2009 Author Share Posted October 9, 2009 ok, and what is the right way to convert the tag contents to html entities, I thought this would work, I was wrong: $my_text= '<pre class="brush: plain"> <div class="header"> <a href="#">home</a> <a href="#">products</a> <a href="#">services</a> </div> </pre>'; echo preg_replace("<pre[^>]*>(.*?)</pre>", htmlentities("$1"), $my_text); Warning: Unknown modifier ']' Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934055 Share on other sites More sharing options...
corbin Posted October 9, 2009 Share Posted October 9, 2009 echo preg_replace("<pre[^>]*>(.*?)</pre>", htmlentities("$1"), $my_text); Ahhh.... It can't be <pre.... as the pattern. It needs delimiters. Also, you'll need to use the e flag and put the function name in quotes if you want to do that. echo preg_replace("~<pre[^>]*>(.*?)</pre>~e", "htmlentities('$1')", $my_text); Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934056 Share on other sites More sharing options...
adrianTNT Posted October 10, 2009 Author Share Posted October 10, 2009 $my_text= '<pre class="brush: plain"> <div class="header"> <a href="#">home</a> <a href="#">products</a> <a href="#">services</a> </div> </pre>'; echo preg_replace("~<pre[^>]*>(.*?)</pre>~e", "htmlentities('$1')", $my_text); This one returns the string unchanged. - Maybe the forum is formating the code differently and I am pasting the wrong thing? - Or the intitial text needs to be in double quotes? Sorry to bother you, I thought it was easyer. Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934058 Share on other sites More sharing options...
adrianTNT Posted October 10, 2009 Author Share Posted October 10, 2009 After some more tests... I think it has to do with new lines in my string ... Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934061 Share on other sites More sharing options...
thebadbad Posted October 10, 2009 Share Posted October 10, 2009 Yep, just add the s modifier after the pattern (I also added a word boundary and made the search case insensitive): preg_replace('~<pre\b[^>]*>(.*?)</pre>~ise', 'htmlentities(\'$1\')', $my_text); If you want to retain the pre tags around the matched content, just grab the tags and use them in the replacement: preg_replace('~(<pre\b[^>]*>)(.*?)(</pre>)~ise', '\'$1\' . htmlentities(\'$2\') . \'$3\'', $my_text); I'm grabbing the last match to be sure the casing is right. Guess it's not necessary though. Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934067 Share on other sites More sharing options...
adrianTNT Posted October 10, 2009 Author Share Posted October 10, 2009 I'm almost there preg_replace('~(<pre\b[^>]*>)(.*?)(</pre>)~ise', '\'$1\' . htmlentities(\'$2\') . \'$3\'', $my_text); Is almost perfect, just that it made html entities of the class="brush: plain" too, how to change that to only start the html entities right after that? Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934068 Share on other sites More sharing options...
thebadbad Posted October 10, 2009 Share Posted October 10, 2009 No it doesn't. But for some reason it escapes the quotes. When I use this instead, it doesn't: preg_replace('~(<pre\b[^>]*>)(.*?)(</pre>)~ise', '"$1" . htmlentities("$2") . "$3"', $my_text); Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934069 Share on other sites More sharing options...
adrianTNT Posted October 10, 2009 Author Share Posted October 10, 2009 Thanks for the help guys, topic solved Quote Link to comment https://forums.phpfreaks.com/topic/177143-solved-need-help-on-a-specific-match-content-of-a-tag/#findComment-934071 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.