Rick Hodder
Rick Hodder

Reputation: 2252

PHP parsing XML with invalid character

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?

Upvotes: 2

Views: 214

Answers (1)

Baba
Baba

Reputation: 95161

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

Upvotes: 3

Related Questions