2017-10-17 11 views
1

2つの文字列を比較することで、一致しない文字または単語(つまり部分文字列)を取得する必要があります。例については2つの文字列を比較し、一致しないサブ文字列を返す

$str1 = 'one {text} three'; // {text} is a keyword to find the position where my substring output is located 
$str2 = 'one two three'; 

//I need to return following output 
$output = 'two'; 

答えて

1

私は私が正規表現パターンと{text}プレースホルダを置き換えるこの近づくだろう。 2番目の文字列にpreg_match_allを使用して一致するセグメントを探します。

$str1 = 'one {text} three {text} five'; 
$str2 = 'one two three four five'; 

$pattern = str_replace('{text}', '([\w]+)', $str1); 

preg_match_all("/{$pattern}/", $str2, $matches); 
var_dump($matches); 
0
$str1 = 'one {text} three'; 
$str2 = 'one two three'; 

$str11 = explode(' ', $str1); 
$str22 = explode(' ' , $str2); 

$result=array_diff($str22,$str11); 

print_r($result); 

これは、あなたの答えのための アレイ([1] => 2)

+0

感謝を出力!しかし、私はこれが正しいアプローチではないと感じ、他の答えはいくつかの解決策を提供します。 –

関連する問題