-2

私はこの配列を持っています:

$GivenString = array("world", "earth", "extraordinary world");

このような変数の「不一致」文字列を取得する方法:

$string = 'hello, world'; // output = 'hello, '
$string = 'down to earth'; // output = 'down to '
$string = 'earthquake'; // output = ''
$string = 'perfect world'; // output = 'perfect '
$string = 'I love this extraordinary world'; // output = 'I love this '

ありがとう!

4

3 に答える 3

1

array_diff http://php.net/manual/en/function.array-diff.php

$tokens = explode(' ', $string);
$difference = array_diff($tokens, $GivenString);
于 2012-12-25T07:47:31.727 に答える
1

str_replaceシンプルで助かると思います

$GivenString = array("world", "earth", "extraordinary");

echo str_replace($GivenString, "", $string);
于 2012-12-25T07:55:44.323 に答える
0

str_replace例にあるように、役に立ちません$string = 'earthquake'; // output = ''。これがあなたの仕事を成し遂げるコードです。

$GivenString = array("world", "earth", "extraordinary world");

foreach ($GivenString as &$string) {
    $string = sprintf('%s%s%s', '[^\s]*', preg_quote($string, '/'), '[^\s]*(\s|)');
}

// case sensitive
$regexp = '/(' . implode('|', $GivenString) . ')/';

// case insensitive
// $regexp = '/(' . implode('|', $GivenString) . ')/i';


$string = 'earthquake';
echo preg_replace($regexp, '', $string);
于 2012-12-25T08:24:43.283 に答える