2

I am getting an xml file from a webservice.

Unfortunately the file contains character code 2 (\u0002), which breaks the parser.

So I'm trying to remove all character code 2's, but I get wierd results just when trying to do a simple string replace.

To test this, I created a php file named test.php with the following content:

<?
$aaa="Hi"+chr(2)+"There"+chr(2)+"Rick";
$aaa = str_replace($aaa,chr(2),"");
echo $aaa;
?>

I'm running this from the command line:

php test.php

When I run this I get a blank string.

What am I doing wrong?

1 Answer 1

3

Yes your result would be black because PHP uses . for concatenation and not +

You should change

$aaa="Hi"+chr(2)+"There"+chr(2)+"Rick";

To

$aaa="Hi".chr(2)."There".chr(2)."Rick";

Also also from PHP DOC

 mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

You should also change

$aaa = str_replace($aaa,chr(2),"");

To

$aaa = str_replace(chr(2),"",$aaa);
Sign up to request clarification or add additional context in comments.

Comments

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.