0

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

}
2
  • 4
    Please post what you expect the pattern to match, and what input strings you have tested which don't match. Commented Jan 17, 2014 at 14:19
  • sure see edit, i've tested "#(?m)//watch-.*?title=#" and "/(?m)/watch-.*?title=/" with no luck. Commented Jan 17, 2014 at 14:24

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Use preg_match_all() if you expect multiple matches.
changed to preg_match_all
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.