从特定文本的字符串中删除链接
I want to remove links (keeping text) from a string only if the text inside the link match exactly a term.
I'm not sure what's wrong with this regexp, but it strips out a lot of text.
$terms = ["justo pulvinar"];
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed <a href=\"#/app/292\">quam nibh, faucibus</a> quam nibh, faucibus eget tortor eget, finibus semper justo. aliquet <a href=\"#/app/212\">justo pulvinar</a>, elit.";
foreach ($terms as $term) {
$string = preg_replace('/<a href=\"(.*?)\">('.$term.')<\/a>/', "\\2",$string);
}
echo $string;
Edit maybe I wasn't clear enough, I have a $terms
array for a reason: I want to remove from $string
links that match exactly those terms, not all links.
Everything looks okay except your replacement operation. This one works for me:
$string = preg_replace('/<a [^>]*>'.$term.'<\/a>/', $term, $string)
Here, we are matching an <a
and all characters after it which are not >
because the attributes of the a
tag are not relevant for the operation. Then we want to see the term we're working on, and a closing a
tag.
Replace all of that with just the term. I'm not using backreferences because there should be no variance in the string values. A scenario where backreferences might be useful is if case were variable and needed to be preserved.
$terms = ["justo pulvinar"];
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed <a href=\"#/app/292\">quam nibh, faucibus</a> quam nibh, faucibus eget tortor eget, finibus semper justo. aliquet <a href=\"#/app/212\">justo pulvinar</a>, elit.";
foreach ($terms as $term) {
$string = strip_tags($term);
echo $string;
}
I have try some regExr and found something I think it will work for you
$terms = ["justo pulvinar"];
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed <a href=\"#/app/292\">quam nibh, faucibus</a> quam nibh, faucibus eget tortor eget, finibus semper justo. aliquet <a href=\"#/app/212\">justo pulvinar</a>, elit.";
$string = preg_replace('/<a\s+(?:[^>]*?\s+)?href=([\'"])(.*?)\1>/', "", $string);
echo $string;