2009-10-22 5 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); 
관련 문제