php
  • str-replace
  • 2010-02-15 12 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?' 
    
    相关问题