bas7320 Posted January 11, 2008 Share Posted January 11, 2008 An example of a string I have passed via a URL is CY5RPLM#V<\\=!EX. I need to always change the "\\" to "\" whenever these appear. I have tried to use the code below to do this but cannot get it to return the value to confirm it has stripped away the extra "\": <?php $newvalue= str_replace("\\","\",CY5RPLM#V<\\=!EX) echo $newvalue ?> Is the fact that I am dealing with "\" the issue? Thanks, Steve Quote Link to comment https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/ Share on other sites More sharing options...
toplay Posted January 11, 2008 Share Posted January 11, 2008 If you're looking for two backslashes in a row and want to replace with one backslash, then do: <?php $newvalue= str_replace("\\\\","\\",'CY5RPLM#V<\\=!EX'); echo $newvalue; ?> Outputs: CY5RPLM#V<\=!EX Quote Link to comment https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/#findComment-436260 Share on other sites More sharing options...
Nhoj Posted January 11, 2008 Share Posted January 11, 2008 Could also use stripslashes().. It reduces double backslashes to single backslashes... If you had 4 consecutive slashes it'd cut it down to two, if you had two slashes it'd replace it with 1. $newvalue = stripslashes($STRING); echo $newvalue; Quote Link to comment https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/#findComment-436263 Share on other sites More sharing options...
bas7320 Posted January 11, 2008 Author Share Posted January 11, 2008 That worked like a charm but when I then modified it to now get the variable text (in this case recid) that is being passed in the URL, I get a parse error on line 2. Here is the code I am using: <?php $oldvalue=<? echo $_REQUEST['recid'] ?>; $newvalue= str_replace("\\\\","\\",'$oldvalue'); echo $newvalue; ?> If you're looking for two backslashes in a row and want to replace with one backslash, then do: <?php $newvalue= str_replace("\\\\","\\",'CY5RPLM#V<\\=!EX'); echo $newvalue; ?> Outputs: CY5RPLM#V<\=!EX Quote Link to comment https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/#findComment-436274 Share on other sites More sharing options...
toplay Posted January 11, 2008 Share Posted January 11, 2008 Learn up on php syntax and when to use PHP opening and closing tags. The error is because you did not close php tags before opening new one. However, there's no need to do this if all of it is php code anyway. So, change this: $oldvalue=<? echo $_REQUEST['recid'] ?>; to this: $oldvalue = $_REQUEST['recid']; Quote Link to comment https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/#findComment-436275 Share on other sites More sharing options...
dsaba Posted January 11, 2008 Share Posted January 11, 2008 this function will strip slashes from your $_REQUEST, $_GET, and $_POST vars only if necessary by detecting your magic quotes settings: No more guesswork. Any server. function stripslash($str) { if (get_magic_quotes_gpc()) { $str = stripslashes($str); } return $str; } Quote Link to comment https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/#findComment-436331 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.