0

これは機能していません。何が起こっているのかはとても単純なはずです:-(

    $test = "hello naughty";


$swearWords = array("naughty","notallowed");

foreach ($swearWords as $naughty)
{
    $post = str_ireplace($naughty, "<b><i>(oops)</i></b>", $test);

}

echo $post;

ありがとう

4

6 に答える 6

3

入力パラメーターとして (常に) $test を使用しています。
この例では、foreach ループの 2 回目の繰り返しで針notallowedが見つからないため、入力文字列$test='hello naughty'が変更されずに返されます。

<?php
$test = "hello naughty";
$swearWords = array("naughty","notallowed");

$post = $test;
foreach ($swearWords as $naughty)
{
    $post = str_ireplace($naughty, "<b><i>(oops)</i></b>", $post);
}

echo $post;

版画hello <b><i>(oops)</i></b>

str_ireplace は最初のパラメーターとして針の配列を取ることができます

<?php
$test = "hello notallowed, this is naughty";
$post = str_ireplace(array("naughty","notallowed"), "<b><i>(oops)</i></b>", $test);
echo $post;

印刷しhello <b><i>(oops)</i></b>, this is <b><i>(oops)</i></b>ます。

于 2012-11-08T10:31:29.743 に答える
1

配列を直接使用して置換できます

<?php

    $test = "hello naughty";
    $swearWords = array("naughty","notallowed");
    $post = str_ireplace($swearWords, "<b><i>(oops)</i></b>", $test);
    echo $post;
?>
于 2012-11-08T10:32:56.933 に答える
1

使用する

$test = "hello naughty";
$swearWords = array("naughty","notallowed");
$post = str_ireplace($swearWords,"<b><i>(oops)</i></b>",$test)
echo $post;

代わりは。関数が配列パラメーターを処理する方法については、str_ireplaceを参照してください。

于 2012-11-08T10:33:05.633 に答える
1

これで試してください

$test = "hello naughty";


$swearWords = array("naughty","notallowed");

foreach ($swearWords as $naughty)
{
//$post = str_ireplace($naughty, "(oops)", $test);
echo str_ireplace($naughty,"<b><i>(oops)</i></b>",$test)."<br>";

}

編集したコードを試す

于 2012-11-08T10:34:32.717 に答える
0

他の回答をサポートするために、役立つ例を示すリンクがいくつかあります。

これらがあなたにとって役に立ち、あなたの問題を解決しますように。

http://www.w3schools.com/php/func_string_str_ireplace.asp

http://php.net/manual/en/function.str-ireplace.php

ありがとう。

于 2012-11-08T10:46:15.607 に答える
-1

$swearWords = array("いたずら"); 次に、いたずらの代わりに (おっと) と言います。

于 2012-11-08T10:32:24.750 に答える