I want to retrieve the entire sentence that surrounds a link, delimited by punctuation (such as . or ! or ? or newline).
The purpose is to provide a better context for the link.
So for example if i have this...
$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 = "https://example.com/my-sentence";
... I need to get to that...
$output = "This is the sentence I want.";
So far, I managed to isolate a sentence that doesn't contain tags, like this:
$input = "I don't want this piece. This is the sentence I want. In don't want this piece either";
$filter = "sentence";
$regex = '/[A-Z][^\\.;]*('.$filter.')[^\\.;]*/';
if (preg_match($regex, $input, $match))
$output = $match[0];
This works just fine. Next, I don't know how to get around the punctuation inside the url.
I explored isolating the anchor first and regexing that, which works on any single example but may generate collisions in the wild (anchors duplicating other anchors or random text).
Another way to go seems to be strip_tags, something like...
$input = strip_tags($input);
... the problem being that I need them both stripped and not stripped at the same time.
Maybe a more specific regex or some smart wrapping of the functions could bring an easy way out of this, or maybe it's a dead end and some other approach is required, I don't know, but right now I'm stuck, please help!