简体   繁体   中英

PHP regular expression not working

I am trying to get those matches from an html file. It has several statements on them looking like this:

links(6)  = "chicas-con-juguetes.asp"

I am trying to extract them with this function:

    public static function extract_all_vid_links($html){
    $pattern="links([0-9])  = \"(.*).asp\"";
    preg_match_all( "/$pattern/i", $html ,$out, PREG_SET_ORDER);

    print_r($out);
    //    foreach($out as $values){
    //       echo $values[0]."<br/>";
    //   }
}

But it is not working!?!? why?

modified:

    $pattern="links\([0-9]\)  = \"(.*).asp\"";
    preg_match_all( "/$pattern/i", $html ,$out, PREG_SET_ORDER);

Still doesn't work.

You need to escape the ( and ) characters if you don't want to create a new match-group, take a look here: links\\(\\d\\) .

This does fix the problem you have described (so your description is wrong probably):

<?php
header('Content-Type: text/plain;');


$html = 'links(6)  = "chicas-con-juguetes.asp"';

$pattern="links\(\d\)  = \"(.*).asp\"";
$r = preg_match_all( "/$pattern/i", $html ,$matches);

var_dump($r, $matches);

Demo , Output:

int(1)
array(2) {
  [0]=>
  array(1) {
    [0]=>
    string(37) "links(6)  = "chicas-con-juguetes.asp""
  }
  [1]=>
  array(1) {
    [0]=>
    string(19) "chicas-con-juguetes"
  }
}
preg_match_all('/links\([0-9]+\)  = \"([^\.]*?).asp\"/i', $html, $out, PREG_SET_ORDER);

You need to escape the () characters. Also, I added a + after [0-9] so that numbers above 9 also work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM