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
https://forums.phpfreaks.com/topic/85482-solved-search-replace-of/
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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.