-1

I want to replace some quotes from an xml doc and use a php function for that. It works fine with all chars, besides this one:

enities

I tried thes unicode chars but did not suceed:

'“', '”', '„', '″'

In my optinion the first one is a: „ or „ but:

echo str_replace( '„', '####', $desc );

does not work. The Result ist: ?Life?.

This is my function for testing:

echo str_replace( '„', '####', $desc );
hakre
  • 193,403
  • 52
  • 435
  • 836
user998163
  • 471
  • 1
  • 9
  • 28
  • This will replace everything except alpha-numeric character : `preg_replace('/[^A-Za-z0-9\-]/', '', $desc);` I hope it can help in your case. – Rayhan Muktader Oct 03 '13 at 20:57
  • @Rayhan Muktader This is not a good solution. A normal Text contains much more than alpha-numeric characters and with this regex, everything gets removed. – user998163 Oct 04 '13 at 00:34
  • can you provide us xml data? – Shushant Oct 06 '13 at 11:15
  • The code example you give in your question is incomplete and does not reproduce the issue you describe. Please fix your question first. - And yes, generally, this is perfectly possible. You only need to provide enough information. Possible related: [PHP DomDocument failing to handle utf-8 characters (☆)](http://stackoverflow.com/q/11309194/367456) – hakre Oct 07 '13 at 08:25
  • If you can access an original file with those strange quotes why don't you copy them directly into the the first argument of str_replace? Or save the whole string before the str_replace if there is any previous conversion and copy from this saved version? – Lajos Veres Oct 07 '13 at 08:47
  • So far you only have posted an image (!) of the problem text, not the source XML file you have that problem with. As Unicode contains some hundreds of thousands ways to write different characters and combined with the possibility of display per each font, it is very hard to say how to solve your problem precisely unless you provide the XML or an excerpt from it as reference. Add that to your question, otherwise it would be continuing to guess around which sounds like the problem in the first place. – hakre Oct 10 '13 at 06:11

4 Answers4

2

Your problem is because symbols you're trying to replace are UTF-8 symbols and take more than 1 byte, str_replace cannot work with such symbols, use MB_#### strings - which suit for multibyte strings, like in following example:

<?php

// NOTE - file is saved as UTF-8 file

mb_internal_encoding('utf-8');
mb_regex_encoding('utf-8');

$s = 'a„b';

echo $s."<br>";

$s = mb_eregi_replace('„', '"', $s);

echo $s."<br>";
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

I'm not sure what your replace command is:

//This one
echo str_replace( '„', '####', $desc );
// or this one?
echo str_replace( '&#8222;', '####', $desc );

The second command will not work as it replaces &#8222; with ####, but I guess you want to replace the unicode character with ####

Also strtr is a nice solution as you can pass an array to it:

// This turns: „Life‟ into &#8222;Life&#8223;
echo strtr($desc, array('„' => '&#8222;', '‟' => '&#8223;'))

And secondly even thought you are not replacing with ####, the character should not be turned into a ? but stay the same, except your xml encoding is not set to the correct encoding.

mjb4
  • 989
  • 1
  • 9
  • 14
0

You can copy paste any number of special character in $pattern_arr with comma seperated and its respective replacements in $replacement_arr with comma seperated.

<?php
$a = ',,Life"';

$pattern_arr = array('/,/','/"/');
$replacement_arr = array('');

$i=0;
while($i<count($pattern_arr)) {
        $a= preg_replace($pattern_arr[$i], $replacement_arr[0] , $a);
        $i++;
}

print_r($a);
?>
shona
  • 124
  • 4
0

How about my solution for „‟ quotes?
It's working for me and it's quite short:

<meta http-equiv="content-type" content="text/html; charset=utf-8">
<?php
$test1='„Life"';
$test2='„Life‟';
echo str_replace('„','+',$test1.'<br />');
echo str_replace('‟','+',$test2);
?>

Result:

+Life"
„Life+
dryhay
  • 79
  • 10