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
答案 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 – 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。