2009-10-22 15 views
6

私は、縮められたアポストロフィを取り除こうとしています(ある種のリッチテキスト文書から貼り付けられたものです)、私はロードブロックに当たっているようです。以下のコードは私のために働いていません。 PHP - 縮んだアポストロフィを取り除く

$word = "Today’s"; 
$search = array('„', '“', '’'); 
$replace = array('"', '"', "'"); 
$word = str_replace($search, $replace, htmlentities($word, ENT_QUOTES)); 

What I end up with is $word containing 'Today’s'. 

私は私の$検索配列からアンパサンドを削除

、置き換えが行われますが、アンパサンドは、文字列中に残されているので、これは、明らかに、仕事を得ることはありません。アンパサンドを渡すときにstr_replaceが失敗するのはなぜですか?

$word = htmlentities(str_replace($search, $replace, $word), ENT_QUOTES); 

+2

これらの波のアポストロフィをスマート引用符といいます。 – random

答えて

9

理由だけでこれをしませんか?

+0

うわー、それは信じられないほど簡単でした。私はあまりにも長い間コーディングしてきたと思います! – Anthony

6

私が適切に動作するためには、@cletusのレイアウト例より少し強固なものが必要でした。ここでは私のために働いた:

// String full of rich characters 
$string = $_POST['annoying_characters']; 

// Replace "rich" entities with standard text ones 
$search = array(
    '“', // 1. Left Double Quotation Mark “ 
    '”', // 2. Right Double Quotation Mark ” 
    '‘', // 3. Left Single Quotation Mark ‘ 
    '’', // 4. Right Single Quotation Mark ’ 
    ''', // 5. Normal Single Quotation Mark ' 
    '&', // 6. Ampersand & 
    '"', // 7. Normal Double Qoute 
    '&lt;', // 8. Less Than < 
    '&gt;'  // 9. Greater Than > 
); 

$replace = array(
    '"', // 1 
    '"', // 2 
    "'", // 3 
    "'", // 4 
    "'", // 5 
    "'", // 6 
    '"', // 7 
    "<", // 8 
    ">" // 9 
); 

// Fix the String 
$fixed_string = htmlspecialchars($string, ENT_QUOTES); 
$fixed_string = str_replace($search, $replace, $fixed_string); 
関連する問題