andz Posted May 22, 2009 Share Posted May 22, 2009 ON, my previous post, I've already found the solution which being shared by those who replied. Here's the solution.. <?php $replace = array( '<' => '<', '>' => '>', '"' => '"' //... ); $str = str_replace(array_keys($replace), $replace, $str); ?> Is there any way to add exception to this like allowing <![CDATA[] tag to be excempted to the filtering but the codes inside the CDATA will be filtered. thank you Quote Link to comment https://forums.phpfreaks.com/topic/159292-need-help-on-str_replace-part-2/ Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 You'd have to use regex. Something like this? $str = preg_replace('/\<(^\!)/', '', $str); Quote Link to comment https://forums.phpfreaks.com/topic/159292-need-help-on-str_replace-part-2/#findComment-840114 Share on other sites More sharing options...
thebadbad Posted May 22, 2009 Share Posted May 22, 2009 How many topics do you have to start about this problem of yours? Please stick to one, as you're leaving a mess of unresolved topics behind. To retain the < and > in CDATA tags, use preg_replace() like jackpf suggests (instead of str_replace()): <?php $str = 'Some text <tag>hey!</tag>. Some CDATA: <![CDATA[testing with another <tag>]]> and that was the end of the CDATA section.'; $replace = array( '~<(?!!\[CDATA\[)~i' => '<', '~(?<!\]\])>~' => '>' ); echo preg_replace(array_keys($replace), $replace, $str); ?> Output: Some text <tag>hey!</tag>. Some CDATA: <![CDATA[testing with another <tag>]]> and that was the end of the CDATA section. And if you once again ask how to do this with str_replace(), without stating any reason as to why you're not taking our advice and using preg_replace(), I might be done helping you. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159292-need-help-on-str_replace-part-2/#findComment-840212 Share on other sites More sharing options...
MadTechie Posted May 22, 2009 Share Posted May 22, 2009 Please stick to one topic per problem, i have already marked one as solevd, and this topic shouldn't even be opened, as its pointless people can see the whole picture and your just get members repeating them selfs, you have had 3 topics about the same question, to sum up, (like EVERYONE is saying) use preg_replace (i have no idea why you keep saying how can i do this with str_replace.. if you really don't wanna use preg_replace then your need to use a few str_replace's instead and it will probably have more problems. Quote Link to comment https://forums.phpfreaks.com/topic/159292-need-help-on-str_replace-part-2/#findComment-840233 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.