使用PHP从数据中提取URL

时间:2012-10-12 13:06:33

标签: php mysql

我有一些脚本从表中提取一些文本并将其转储到文本文件中。我希望你的帮助只删除URL,以便文本文件只显示URL。

{
$result = @mysql_query("SELECT post_content_filtered FROM wp_posts ORDER BY post_date desc limit 200");
$tsv  = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM)){
    $tsv[]  = implode("\t", $row);
    $html[] = "<tr><td>" .implode("</td><td>", $row) ."</td></tr>";
}

$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";
$fileName = 'finishedflippa.txt';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
echo $tsv;

post_content_filtered文字看起来像这样:

  blahblahblah http://www.example.com blahblahblah
  blahblahblah http://www.12345.com blahblahblah
  blahblahblah http://www.gfds.com blahblahblah
  blahblahblah http://www.45tyhju.com blahblahblah

blahblahblah每行都相同。非常感谢。

1 个答案:

答案 0 :(得分:6)

网址非常complex definition

您可以尝试与preg_match_all

进行简单匹配
$str = 'text looks something like this: 
blahblahblah http://www.example.com blahblahblah 
blahblahblah http://www.12345.com blahblahblah 
blahblahblah http://www.gfds.com blahblahblah 
blahblahblah http://www.45tyhju.com blahblahblah ' ;

preg_match_all('!https?://[\S]+!', $str, $matches);
var_dump($matches[0]);