Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove "�" from php string? [duplicate]

Possible Duplicate:
How to replace � in a string

I am reading data from an XML sheet coming out of a database. In the raw output I am coming accross this character "�" which is the UTF-8 string meaning "�". Doing a simple search and remove with str_replace does not do the trick when searching for "�" or "�". Is there any other way to remove this character from a string?

UPDATE:

For reference this is the function that is cleaning up strings for me.

    function db_utf8_convert($str)
{
    $convmap = array(0x80, 0x10ffff, 0, 0xffffff);
    return preg_replace('/\x{EF}\x{BF}\x{BD}/u', '', mb_encode_numericentity($str, $convmap, "UTF-8"));
}
like image 677
labago Avatar asked Dec 27 '12 19:12

labago


2 Answers

You can do this:

$str = 'UTF-8 string meaning "�"';
echo preg_replace('/\x{EF}\x{BF}\x{BD}/u', '', iconv(mb_detect_encoding($str), 'UTF-8', $str));

Output: UTF-8 string meaning ""

like image 125
PhearOfRayne Avatar answered Nov 05 '22 19:11

PhearOfRayne


You could do something similar to this:

<?php
$string = "asd fsa fsaf sf � asdfasdfs";

echo preg_replace("/[^\p{Latin} ]/u", "", $string);

Check out this script for more character matches:
http://www.regular-expressions.info/unicode.html#script

EDIT

I did find, this, people says it works, you could give it a try:

<?php
function removeBOM($str=""){
    if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
        $str=substr($str, 3);
    }
    return $str;
}
?>
like image 2
Get Off My Lawn Avatar answered Nov 05 '22 21:11

Get Off My Lawn