を使用した例を次に示しpreg_match()
ます。検索文字列は で引用する必要があることに注意してくださいpreg_quote()
。
$text1 = $_POST['text_area1'];
$text2 = $_POST['text_area2'];
// split search texts. in this case we use new line
$search = explode("\n", preg_quote($text1));
// now prepare for a regex
$regex = '~(' . implode('|', $search) . ')~';
// now split the text by newline
$lines = explode("\n", $text2);
foreach($lines as $line) {
if(preg_match($regex, $line)) {
print $line . "\n";
}
}
出力:
this is content1
this content3
検索文字列を区切る方法を絞り込むことができることに注意してください。私の例では、改行で分割していますが、さらにスペースで分割したり、,
...