0
<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?

Edited by mayanktalwar: n/a

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by d5e5
0

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 :))

Edited by Devoted Hosting: n/a

0

as expected print_r($out) throws empty array

Edited by mayanktalwar: n/a

0

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.

0

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
//        )
//
//)
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.