繁体   English   中英

如何从PHP的tweet或字符串中捕获URL,使用了parse_url但它不起作用

[英]How to catch a URL from a tweet or string in PHP , used parse_url but it doesnt work

好的,我想动态地自动分析应用程序中更新的推文中的URL,我尝试使用PHP中的parse_url函数来分析它们,但它没有返回我期望的结果。.我基本上只想捕获URL来自推文

示例:约翰·麦凯恩(John McCain)最新的推文说:

Must-read Jackson Diehl: "What the #Iraq war taught me about #Syria" http://t.co/7YHYQY6bW0

在使用parse_url捕获来自该tweet的URL之后,它返回了这个

array(2) { ["path"]=> string(35) "Must-read Jackson Diehl: "What the " ["fragment"]=> string(55) "Iraq war taught me about #Syria" http://t.co/7YHYQY6bW0" }

我基本上只想在同一条Tweet中捕获http://t.co/7YHYQY6bW0或任何其他多个URL ...我该如何在PHP中做到这一点?

parse_url用于拆分URL本身。 您可能想要做类似preg_match_all的操作,因为一条推文中可能有多个URL。

<?php
    $string = 'Must-read Jackson Diehl: "What the #Iraq war taught me about #Syria" http://t.co/7YHYQY6bW0';

    preg_match_all('!http[s]?://[^ ]+!',$string,$urls);

    print_r($urls);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM