andyy15
andyy15

Reputation: 41

How do I extract links with a specific domain name using PHP and Regex?

I am trying to extract urls that contain www.domain.com from a database column that contains HTML. The regex has to filter out www2.domain.com instances and external urls like www.domainxyz.com. It should only search for properly coded anchor links.

Here is what I have so far:

<?php
    $content = '<html>
    <title>Random Website</title>
    <body>
        Click <a href="http://domainxyz.com">here</a> for foobar
        Another site is http://www.domain.com
        <a href="http://www.domain.com/test">Test 1</a>
        <a href="http://www2.domain.com/test">Test 2</a>
        <Strong>NOT A LINK</strong>
    </body>
    </html>';

    $regex = "((https?)\:\/\/)?";
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,4})"; 
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?";
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; 
    $regex .= "([www\.domain\.com])";

    $matches = array(); //create array
    $pattern = "/$regex/";

    preg_match_all($pattern, $content, $matches); 

    print_r(array_values(array_unique($matches[0])));
    echo "<br><br>";
    echo implode("<br>", array_values(array_unique($matches[0])));
?>

I am looking for this to find and output only http://www.domain.com/test.

How can I modify my Regex to accomplish this?

Upvotes: 3

Views: 1635

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626709

Here is a much safer way to extract the a href attribute values containing www.domain.com where the key is the XPath '//a[contains(@href, "www.domain.com")]':

$html = "YOUR_HTML_STRING"; // Your HTML string
$dom = new DOMDocument;    
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$arr = array();
$links = $xpath->query('//a[contains(@href, "www.domain.com")]');

foreach($links as $link) { 
   array_push($arr, $link->getAttribute("href"));
}

print_r($arr);

See IDEONE demo, result:

Array
(
    [0] => http://www.domain.com/test
)

As you see, you can use the DOMDocument and DOMXPath with a string, too.

The code is self-explanatory, the XPath expression just means find all <a> tags that have a href attribute containing www.domain.com.

Upvotes: 4

Related Questions