Q: matching URL to a pattern using php I have to use crawlers for my project.
I've used simple dom class to get all the links from a page.
Now I want to filter only those links which are of the form "/questions/3904482/<title of the question".
Here's my attempt:
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://stackoverflow.com/questions?sort=newest');
$pat='#^/question/([0-9]+)/#';
foreach($html->find('a') as $link)
{
    echo preg_match($pat, $link->href);
    {
        echo $link->href."<br>";
    }
}

All the links get filtered out.

A: you say the url is question*s* but your pattern shows no s
Also, it looks like you should be using if not echo
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://stackoverflow.com/questions?sort=newest');
$pat='#^/questions/([0-9]+)/#';
foreach($html->find('a') as $link)
{

    if ( preg_match($pat, $link->href) )
    {
        echo $link->href."<br>";
    }
}


A: You can take advantage of DOM and XPath:
<?php

$dom = new DOMDocument;
@$dom->loadHTMLFile('http://stackoverflow.com/questions?sort=newest');
$xpath = new DOMXPath($dom);
$questions = $xpath->query("//a[contains(@href, '/questions/') and not(contains(@href, '/tagged/')) and not(contains(@href, '/ask'))]");

foreach ($questions as $question) {
    print "{$question->getAttribute('href')} => {$question->nodeValue}";
}

