php
  • str-replace
  • 2010-02-15 73 views 0 likes 
    0

    我想在字符串中替換單引號(')。str_ireplace不會使用單引號

    顯然,這是不行的...:

    $patterns = array(); 
    $replacements = array(); 
    $patterns[0] = "'"; 
    $patterns[1] = '\''; 
    $replacements[0] = 'Something'; 
    $replacements[2] = 'Same thing just in a other way'; 
    
    +1

    哪裏'str_ireplace' ? – kennytm 2010-02-15 15:08:52

    回答

    0

    它看起來像你的示例代碼已經過匿名(索引0 & 2 $替代品?),並在被截斷(其中更換(")是str_ireplace調用)但是...我會猜測你還沒有完全理解str_ireplace。

    第一點是str_ireplace不起作用。它的返回值是字符串的變化字符串/數組。

    第二點是,當你有一個搜索和替換的數組時,PHP將通過從每個數組中取一個項目並將其應用到主題的主題/數組,然後再移動到每個項目的下一個項目數組,然後將其應用於相同的主題。你可以在下面的例子中看到這一點,在這個例子中,兩個主題都被「」替換爲「某種東西」,而「只是以其他方式相同的東西」從未出現在結果中。

    $patterns = array();
    $replacements = array();
    $patterns[0] = "'";
    $patterns[1] = '\'';
    $replacements[0] = 'Something';
    $replacements[1] = 'Same thing just in a other way';

    $subjects[0] = "I've included a single quote.";
    $subjects[1] = "This'll also have a quote.";

    $newSubjects = str_ireplace($patterns, $replacements, $subjects);

    print_r($newSubjects);

    當運行此給出

    陣列([0] => ISomethingve包括一個單引號。[1] => ThisSomethingll也有一個報價。)

    2

    更換(')與(")對我來說工作正常str_ireplace

    $test = str_ireplace("'", "\"", "I said 'Would you answer me?'"); 
    echo $test; // I said "Would you answer me?" 
    

    而且工作正常('

    $test = str_ireplace("\"", "'", "I said \"Would you answer me?\""); 
    echo $test; // I said 'Would you answer me?' 
    
    相關問題