I was not entirely clear on what your text looks like, vs what you want to match against, but I will do my best to try and get it right.
Basically what I am doing here is looking for an opening link tag <a
, followed by some stuff (anything except a closing HTML tag), followed by the text dead host
wrapped in tildas ~
. Then some more stuff, followed by the closing link tag </a>
.
$string = "<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>";
if (preg_match('%<a[^>]*?~dead host~.*?</a>%i', $string)) {
print "Circle up the wagons - a match was found!";
}
else {
print "Let's pitch camp here - no match was found!";
}
Here is an explanation of the REGEX:
% <a [^>]*? ~dead host~ .*? </a> % i
^ ^ ^ ^ ^ ^ ^ ^
1 2 3 4 5 6 7 8
%
Delimiter - Tells the script that the pattern starts here.
<a
Look for an opening link tag.
[^>]*?
This is a character class []
telling the script to find any character that is not ^
a closing html tag >
, as many times as you can *
, up until you hit the next part of the expression ?
. In this case, it will stop when it finds ~dead host~
. This is similar to item #5, except that we want it to match any characters except a closing HTML tag, whereas in number #5, it can match any character, including the closing HTML tag.
~dead host~
Look for the literal string 'dead host' wrapped in tildas '~'.
.*?
This means find any character .
, as many times as you can *
, up until you hit the next part of the expression ?
. In this case, it is </a>
.
</a>
Look for a closing link tag.
%
Delimiter - Tells the script that the pattern ends here.
i
Pattern modifier - Tells the script to ignore the case. If you are searching through multiple lines instead of just one line, you may want to add the ms
flags as well. So instead of your pattern modifier looking like this: i
, it will look like this: ims
. Although this is not technically correct, generally speaking, this has the effect of treating your text as one line, even if you have multiple lines.
Hopefully this is what you were looking for. If I was off in my understanding of what you were looking for, let me know and I can make an edit to adjust it to get you what you want.
Here is a working demo
EDIT:
In response to your comment, you can use preg_replace
instead of preg_match
to replace stuff.
$string = "
<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
<a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
<a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
";
$string = preg_replace('%<a[^>]*?~dead host~.*?</a>%i', ' ', $string);
print $string;
This will replace all of the matches with a space instead of just matching them.
Here is a working demo of the replacement