Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/159292-need-help-on-str_replace-part-2/
Share on other sites

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 :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.