5

I need to replace \ with '' in following string.

$x = "FBU4061\0258";

When I do $x = str_replace('\\', '', $x);

It returns FBU40618,

Is there any workaround to get FBU40610258.

Note: I need to do this inside double quotes. Within single quotes it returns the desired value without problem.

17
  • php.net/manual/en/function.stripslashes.php Commented Nov 9, 2015 at 5:05
  • @RensTillmann, It does not work with double quotes, please see my note Commented Nov 9, 2015 at 5:07
  • Then create a variable and then do whatever you want? I don't understand why that would make a difference. Commented Nov 9, 2015 at 5:08
  • I think that the point where it's in double quotes is where it becomes too late. As soon as you use the variable, \025 is converted to a character: eval.in/465229 Commented Nov 9, 2015 at 5:09
  • @RensTillmann, When you use double quoted strings, PHP does an escape sequence replacement, and \0 is the NUL byte. Commented Nov 9, 2015 at 5:09

3 Answers 3

5

What's probably confusing you here is that double quoted strings interpret this string very differently from a single quoted string. Your string "FBU4061\0258" in PHP will interpret the \02 part of your string as an STX (or start of text) character (i.e. ASCII character 02).

Just try run the code var_dump("FBU4061\0258"); in PHP and see for yourself how the output is not what you would expect. You can even do var_dump(bin2hex("FBU4061\0258")); and see the hexadecimal representation of your string for further clarification...

Example

var_dump(bin2hex('\02'));
// string(6) "5c3032"
var_dump(bin2hex("\02"));
// string(2) "02"

See the difference?

This is all thoroughly documented behavior in the manual.

\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation

So in order to get a string literal of FBU4061\0258 you must escape the backslash inside of double quoted strings.

$x = "FBU4061\\0258";
$x = str_replace('\\', '', $x);
var_dump($x); //string(11) "FBU40610258"

Notice this is because you are placing a string literal in your code. If this string were retrieved from your database, however, this interpolation wouldn't take place, because it's already a string literal.

Again ...

var_dump("FBU4061\\0258"); // string(12) "FBU4061\0258"
var_dump("FBU4061\0258");  // string(9) "FBU40618"

Look at the obvious difference in the length of the string!

Sign up to request clarification or add additional context in comments.

1 Comment

I understand the problem and we are evaluating possible solutions. Thanks for the post
2

Your best chance is to do a mysql replace query:

Change the value in the query:

SELECT REPLACE('http://yourdomain.com', 'http', 'https');

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace

You might also try this instead:

UPDATE table SET field_name = replace(field, 'http, 'https')

https://dba.stackexchange.com/questions/2261/how-do-i-remove-backslashes-from-records-in-mysql

Comments

0

use php stripslashes

echo stripslashes('FBU4061\0258');

or try this code :-

function removeslashes($string)
{
    $string=implode("",explode("\\",$string));
    return stripslashes(trim($string));
}

$text="FBU4061\0258";
echo removeslashes($text);

Stripslashes more details

4 Comments

As I told above it can be done with single quotes, I need to use double quotes. If you use double quotes, you will see it does bot work
That doesn't have to be a problem?
@RensTillmann, Please try and see, actually above value comes from the database and so it come with double quotes
@Rahautos the example in the question uses double quotes so this is not an accurate answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.