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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mirlo
  • 625
  • 9
  • 26

2 Answers2

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.

Amessihel
  • 5,891
  • 3
  • 16
  • 40
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

Nick
  • 138,499
  • 22
  • 57
  • 95