andz Posted May 22, 2009 Share Posted May 22, 2009 how do i use htmlentities in str_replace $val = 'their body'; str_replace('body', 'bodies', $val); Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/ Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 str_replace('body', 'bodies', htmlentities($val)); Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840010 Share on other sites More sharing options...
andz Posted May 22, 2009 Author Share Posted May 22, 2009 what im trying to achieve is that $val = '<script>alert("test")</script>'; str_replace('', '', $val); how am i going to apply htmlentities on str_replace to convert the <>" into htmlentities Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840013 Share on other sites More sharing options...
thebadbad Posted May 22, 2009 Share Posted May 22, 2009 Simply running htmlentities($val) will convert the characters in $val and return the edited string. Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840021 Share on other sites More sharing options...
andz Posted May 22, 2009 Author Share Posted May 22, 2009 @thebadbad: i tried applying that solution but there's an affected script and wont work... but when i tried applying str_replace it's working... so i thought that i can apply htmlentities inside str_replace but i don't know how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840025 Share on other sites More sharing options...
thebadbad Posted May 22, 2009 Share Posted May 22, 2009 but there's an affected script and wont work Huh? Do you mean that you only want to convert certain characters, like < and >? Using str_replace(): <?php $val = str_replace('<', '<', $val); $val = str_replace('>', '>', $val); ?> If that's not what you want, please explain your problem better. Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840030 Share on other sites More sharing options...
andz Posted May 22, 2009 Author Share Posted May 22, 2009 im trying to convert all of the <>"'/\ and other html entity Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840041 Share on other sites More sharing options...
thebadbad Posted May 22, 2009 Share Posted May 22, 2009 Well, htmlentities() converts all applicable characters. What went wrong when using htmlentities()? If you've got a set of characters you want to convert, just run them through str_replace(): <?php $replace = array( '<' => '<', '>' => '>', '"' => '"' //... ); $str = str_replace(array_keys($replace), $replace, $str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840056 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 I don't understand, why can't you just use htmlentities()? Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840074 Share on other sites More sharing options...
andz Posted May 22, 2009 Author Share Posted May 22, 2009 not using it because i'm just using a ready made script and when using htmlentities() it cause a problem on xajax. but when i applied str_replace() the problem didn't occured. just finding a solution to convert all of the html entity into their respective value and put it in str_replace() just like THEBADBAD solution. Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840083 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 Wouldn't it be simpler to find out what's breaking your ajax? Quote Link to comment https://forums.phpfreaks.com/topic/159273-how-to-use-str_replace-and-htmlentities/#findComment-840094 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.