これは機能していません。何が起こっているのかはとても単純なはずです:-(
$test = "hello naughty";
$swearWords = array("naughty","notallowed");
foreach ($swearWords as $naughty)
{
$post = str_ireplace($naughty, "<b><i>(oops)</i></b>", $test);
}
echo $post;
ありがとう
これは機能していません。何が起こっているのかはとても単純なはずです:-(
$test = "hello naughty";
$swearWords = array("naughty","notallowed");
foreach ($swearWords as $naughty)
{
$post = str_ireplace($naughty, "<b><i>(oops)</i></b>", $test);
}
echo $post;
ありがとう
入力パラメーターとして (常に) $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>
ます。
配列を直接使用して置換できます
<?php
$test = "hello naughty";
$swearWords = array("naughty","notallowed");
$post = str_ireplace($swearWords, "<b><i>(oops)</i></b>", $test);
echo $post;
?>
使用する
$test = "hello naughty";
$swearWords = array("naughty","notallowed");
$post = str_ireplace($swearWords,"<b><i>(oops)</i></b>",$test)
echo $post;
代わりは。関数が配列パラメーターを処理する方法については、str_ireplaceを参照してください。
これで試してください
$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>";
}
編集したコードを試す
他の回答をサポートするために、役立つ例を示すリンクがいくつかあります。
これらがあなたにとって役に立ち、あなたの問題を解決しますように。
http://www.w3schools.com/php/func_string_str_ireplace.asp
http://php.net/manual/en/function.str-ireplace.php
ありがとう。
$swearWords = array("いたずら"); 次に、いたずらの代わりに (おっと) と言います。