0

段落をスキャンして、その中の針を別の単語に置き換えたい。例えば

$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

どうすればいいですか?

4

2 に答える 2

1

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"
于 2013-02-05T15:35:47.013 に答える
1

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);
于 2013-02-05T15:57:18.267 に答える