<a href=\".\/(viewtopic.php\?f=15&amp;t=[0-9]+)\" class=\"topictitle\">([#\w\s_\-!$%&()?.,'\":;{}\/\\|’=\[\]+\–\™ ]+)<\/a>

i have this regex ina ction when i use it in software like regex buddy its ok but when i use it in php it just dont work it .throw no error at all..but it produce zero result.
it is made to match this

<a href="./viewtopic.php?f=15&amp;t=119871" class="topictitle">Twilight Graphic Novel</a>

the match is ok but when i write
this

<?php
$contents = file_get_contents($url);

preg_match_all("/<a href=\".\/(viewtopic.php\?f=15&amp;t=[0-9]+)\" class=\"topictitle\">([#\w\s_\-!$%&()?.,'\":;{}\/\\|’=\[\]+\–\™ ]+)<\/a>/i",$contents,$out);


$len=count($out[0]);
echo $len;
?>

$len outputs is zero although when i used this in regexbuddy with the sample page source it worked like a charm..
any ideas?

What is the output when you run:

print_r($out)

Knowing that output can sometimes give a great insight into where these sorts of problems lie (or at least it has been useful when I've been doing regex work :))

as expected print_r($out) throws empty array

Do you get anything different if you do:

preg_match_all("/<a href=\".\/(viewtopic.php\?f=15&amp;t=[0-9]+)\" class=\"topictitle\">(.+?)<\/a>/i",$contents,$out);

Just wondering whether a non-greedy ".+" match might be better. If that doesn't it, it probably means it's something in the first regex match section.

Are you sure $contents contains the string you say it does? When I manually assign the string you want to match to $contents the match works and there is something in the $out array.

<?php
$contents = <<<END
<a href="./viewtopic.php?f=15&amp;t=119871" class="topictitle">Twilight Graphic Novel</a>
END;
preg_match_all("/<a href=\".\/(viewtopic.php\?f=15&amp;t=[0-9]+)\" class=\"topictitle\">([#\w\s_\-!$%&()?.,'\":;{}\/\\|’=\[\]+\–\™ ]+)<\/a>/i",$contents,$out);

$len=count($out[0]);
echo '$len is equal to ' . "$len\n";
print_r($out);

//Gives the following output:
//$len is equal to 1
//Array
//(
//    [0] => Array
//        (
//            [0] => <a href="./viewtopic.php?f=15&amp;t=119871" class="topictitle">Twilight Graphic Novel</a>
//        )
//
//    [1] => Array
//        (
//            [0] => viewtopic.php?f=15&amp;t=119871
//        )
//
//    [2] => Array
//        (
//            [0] => Twilight Graphic Novel
//        )
//
//)
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.