0

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.

2 Answers 2

1

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 dot
  • website\.fr : your website
  • ([.\/][^"]*)?: the URL may finish by a slash followed by anything except a quote

This 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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";

Demo on 3v4l.org

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.