段落をスキャンして、その中の針を別の単語に置き換えたい。例えば
$needles = array('head', 'limbs', 'trunk');
$to_replace = "this";
$haystack = "Main parts of human body is head, Limbs and Trunk";
最終出力が必要
Main part of human body is this, this and this
どうすればいいですか?
段落をスキャンして、その中の針を別の単語に置き換えたい。例えば
$needles = array('head', 'limbs', 'trunk');
$to_replace = "this";
$haystack = "Main parts of human body is head, Limbs and Trunk";
最終出力が必要
Main part of human body is this, this and this
どうすればいいですか?
PHPを使用していると仮定すると、を試すことができますstr_ireplace
。
$needles = array('head', 'limbs', 'trunk');
$to_replace = "this";
$haystack = "Main parts of human body is head, Limbs and Trunk";
echo str_ireplace($needles, $to_replace, $haystack); // prints "Main parts of human body is this, this and this"
preg_replace を使用:
$needles = array('head', 'limbs', 'trunk');
$pattern = '/' . implode('|', $needles) . '/i';
$to_replace = "this";
$haystack = "Main parts of human body is head, Limbs and Trunk";
echo preg_replace($pattern, $to_replace, $haystack);