0

I try myself to extract this type of links using explode but it not work maybe regular expression will helpful

links are :

01 &#8211; Root Books <a href="http://mylinkforgoogle.com.pdf" 

shokly  <a href="http://mylinkforgoogle.com.pdf"

i have dozen links in text file that i want extract i have no good knowldge but i want to try

php regular expression for start with number or alphabet mid contain <a href=" at end with .pdf 
Bilal Raj
  • 11
  • 1
  • 7

3 Answers3

0
 <?php

    $text = 'shokly  <a href="http://mylinkforgoogle.com.pdf'; 
    $link = strstr($text, 'http://');
    echo$link;

    ?>

Output:

http://mylinkforgoogle.com.pdf

I think this will be enough but if you are looking for pattern; it must be like this:

   <?php

$text = 'shokly  <a href="http://mylinkforgoogle.com.pdf"'; 
$pattern="/http:\/\/.[^w]*?\.pdf/i";
preg_match($pattern, $text, $matches);
print_r($matches);

?>

Output:

Array ( [0] => http://mylinkforgoogle.com.pdf )
BARIS KURT
  • 477
  • 4
  • 15
0

You can use a regex like this:

href="(.*?)"

Working demo

Code

$re = "/href=\"(.*?)\"/"; 
$str = "01 &#8211; Root Books <a href=\"http://mylinkforgoogle.com.pdf\" \n\nshokly  <a href=\"http://mylinkforgoogle.com.pdf\"\n"; 

preg_match_all($re, $str, $matches);
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

Surely you're not parsing html with regex?

Here's a couple regexes that will do the job:

// @todo: This should be possible in 1 line
preg_match('/(?<=href=").*?(?=")/', $input_line, $output_array);
preg_match("/(?<=href=').*?(?=')/", $input_line, $output_array);

You can test them out here.

Community
  • 1
  • 1
lostphilosopher
  • 4,361
  • 4
  • 28
  • 39