Dean
Dean

Reputation: 517

php regex not working, works in c#

could someone tell me why this regex does not work in php, but the pattern works in c#?

I'm trying to replicate a working function that i have in c# and i tried to use the same regex pattern that i used there, it does not return any results, i checked google and understand that php regex uses a different set of delimiters but now i have no idea why it won't work.

php:

$results_q = "#(?m)/watch-.*?title=#";
preg_match($results_q, $html, $results, PREG_PATTERN_ORDER);    

working in c#:

var results = Regex.Matches(test, "(?m)/watch-.*?title=");

how can i make it work in php? what are the differences in the pattern?

regex subject:

<a href="/watch-2742524-Thor-The-Dark-World" title="Watch Thor The Dark World (2013)">

output should be: /watch-2742524-Thor-The-Dark-World

This is my php function and it's currently working but only returning one result, i expect alot! as the page has several items on it matching my pattern.

function parseURL($url, $page, $featured = false) {

    $opts = array(
        'http'=>array(
                'method'=>"GET",
                'header'=>"Accept-language: en\r\n"
                )
            );

    $context = stream_context_create($opts);

    $html = file_get_contents($url, false, $context);   
    $results_q = "/(?m)watch-.*?title=/";

    preg_match($results_q, $html, $results);    


    echo var_dump($results);

}

Upvotes: 0

Views: 84

Answers (2)

TtheT
TtheT

Reputation: 96

This worked for me.

Keep it simple, the \s* is there due to unseen whitespace that may be there, also tried to avoid case sensitivity and stuff...

$results_q = "#href="(/watch.)"\stitle#Uisx";

preg_match($results_q, $html, $results);

But was just a quicky try on my mobile...

Check this two websites

php preg_matc

php regex tester

Upvotes: 0

Vlad Nikitin
Vlad Nikitin

Reputation: 1951

You are using wrong flag (last parameter of preg_match), http://www.php.net/manual/ru/function.preg-match.php here as You can see this one PREG_OFFSET_CAPTURE is allowed. You can just omit this last param

preg_match($results_q, $html, $results); 

also i have modified a little bit you pattern , the rusult:

$html = "<a href=\"/watch-2742524-Thor-The-Dark-World\" title=\"Watch Thor The Dark World (2013)\">";
$html .= "<a href=\"/watch-asdf742524-Thor-The-Dark-World\" title=\"Watch Thor The Dark World (2013)\">";

$results_q = "#(?m)(/watch-[^\"]*).*?title=#i";
$res = preg_match_all($results_q, $html, $results);
var_dump($results);

Upvotes: 2

Related Questions