Jump to content

[SOLVED] Search & Replace of "\"


bas7320

Recommended Posts

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.