-2

I have the following code:

$regex='|<a.*?href="(.*?)"|';      //PARSE FOR LINKS
preg_match_all($regex,$result,$parts);
$links=$parts[1];

foreach($links as $link){
    echo $link."<br>";
}

Its output is the following:

/watch/b4se39an
/watch/b4se39an
/bscsystem
/watch/ifuyzwfw
/watch/ifuyzwfw
/?sort=v
/?sort=c
/?sort=l
/watch/xk4mvavj
/watch/2h7b53vx
/watch/d7bt47xb
/watch/yh953b17
/watch/tj3z6ki2
/watch/sd4vraxi
/watch/f2rnthuh
/watch/ey6z8hxa
/watch/ybgxgay1
/watch/3iaqyrm1
/help/feedback

How I can use a regular expression to extract the /watch/..... strings?

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Xtal
  • 295
  • 5
  • 20

1 Answers1

2

Modify your regex to include the restriction on /watch/:

$regex = '|<a.*?href="(/watch/.*?)"|'; 

A simple test script can show that it's working:

$tests = array( "/watch/something", "/bscsystem");
$regex = '|<a.*?href="(/watch/.*?)"|'; 

foreach( $tests as $test) {
    $link = '<a href="' . $test . '"></a>';
    if( preg_match( $regex, $link))
       echo $test . ' matched.<br />';
}

This will produce:

/watch/something matched.
nickb
  • 59,313
  • 13
  • 108
  • 143