- array_map: documentation ( source)
- print_r: documentation ( source)
- preg_match_all: documentation ( source)
- strip_tags: documentation ( source)
- preg_quote: documentation ( source)
<?php
//$input = "I don't want this piece. This is the sentence I want. In don't want this piece either";
$input = "I don't want this piece! This is the <a href='https://example.com/my-sentence'>sentence</a> I want. In don't want this piece either";
$filter = "sentence";
$regex = '~\b(?:[^.?!]|https?://[^<>\s"\']++)*?'.preg_quote($filter, '~').'(?:[^.?!]|https?://[^<>\s"\']++)*~u';
if (preg_match_all($regex, $input, $match)){
print_r( array_map(function($x) {return strip_tags($x);}, $match[0]) );
}