php正则表达式以数字或字母开头,包含<a href="

时间:2015-12-17 21:24:23

标签: php regex

<p>I try myself to extract this type of links using explode but it not work maybe regular expression will helpful </p> <p>links are :</p> <pre><code>01 &#8211; Root Books <a href=" http:="" mylinkforgoogle.com.pdf"="" shokly="" <a="" href="http://mylinkforgoogle.com.pdf" <="" code="">

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 

3 个答案:

答案 0 :(得分:0)

 <?php

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

    ?>

输出:

http://mylinkforgoogle.com.pdf

我认为这样就足够了,但如果你正在寻找模式;它一定是这样的:

   <?php

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

?>

输出:

Array ( [0] => http://mylinkforgoogle.com.pdf )

答案 1 :(得分:0)

你可以使用这样的正则表达式:

href="(.*?)"

<强> Working demo

代码

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

答案 2 :(得分:0)

当然你不是parsing html with regex

这里有几个正在执行此任务的正则表达式:

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

您可以测试它们here