Reputation: 674
There are many questions like this question but I can not find exact answer. And I am unfamiliar Regular Expresion topic.
PHP7 : I want to check if $str contains a html code and its href
refer to the url : "website.fr"
like '<a href="www.website.fr/123/">*****</a>'
i used the pattern <\b[a>]\S*\<\/a>
but is not working.
Any help please.
Upvotes: 0
Views: 35
Reputation: 147146
In general, parsing HTML with regex is a bad idea (see this question). In PHP, you can use DOMDocument
and DOMXPath
to search for elements with specific attributes in an HTML document. Something like this, which searches for an <a>
element somewhere in the HTML which has an href
value containing the string 'website.fr/'
:
$html = '<a href="www.website.fr/123/">*****</a>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
if (count($xpath->query("//a[contains(@href, 'website.fr/')]")))
echo "found";
else
echo "not found";
Upvotes: 1
Reputation: 6374
This regexp catches an a
element with an href
attribute which refers to a website.fr
url:
<a.*\shref="([^"]*\.)?website\.fr([.\/][^"]*)?"
Explanation:
<a[^>]*
: an anchor beginning\shref="
: ...followed by an opened href attribute([^"]*\.)?
: the URL may begin by anything except a quote and finishing by a dotwebsite\.fr
: your website([.\/][^"]*)?
: the URL may finish by a slash followed by anything except a quoteThis regexp may not cover all cases (for example an URL containing a quote). Generally, it's discouraged to parse HTML with regexes. Better use a XML parser.
Upvotes: 1