Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取带有超链接的文本字符串,将其展开,然后在php中重新插入?_Php_Twitter_Hyperlink - Fatal编程技术网

如何获取带有超链接的文本字符串,将其展开,然后在php中重新插入?

如何获取带有超链接的文本字符串,将其展开,然后在php中重新插入?,php,twitter,hyperlink,Php,Twitter,Hyperlink,如果我有来自Twitter的一串文本,例如: $string = "This is a link http://t.co/252351ga to follow."; 如何展开链接,并按如下方式重新插入原始字符串: $new_string = "This is a link http://www.example.org/article/23534 to follow."; 您可能希望使用preg_replace之类的东西,并使用模式匹配URL,并用您自己的URL替换匹配 编辑: 您可以在此处找

如果我有来自Twitter的一串文本,例如:

$string = "This is a link http://t.co/252351ga to follow.";
如何展开链接,并按如下方式重新插入原始字符串:

$new_string = "This is a link http://www.example.org/article/23534 to follow.";

您可能希望使用preg_replace之类的东西,并使用模式匹配URL,并用您自己的URL替换匹配

编辑: 您可以在此处找到一个好的URL regexp:

然后按如下方式使用:

$string = "This is a link http://t.co/252351ga to follow.";
$pattern = ''; //the pattern from the link
$replacement = 'http://www.example.org/article/23534';
echo preg_replace($pattern, $replacement, $string);

这是一个由两部分组成的问题。首先,您需要一个正则表达式来查找和提取URL。为了简单起见,最好使用preg_replace_回调。(如果使用搜索,您可以找到任意数量的更好的正则表达式。)


其次,您可以使用缩短的URL扩展服务/API,或者自己请求URL,并在响应中检查重定向或相应的元标记。(例如:-忘记了我最近读到的服务名称..没关系,它只是简单地称为“LongURL.org”。没有关联。)

打开它们?正如在“取消排序”中一样,您指向的是真实的url而不是更短的url?是的,这正是我想要做的。
 = preg_replace_callback('#http://\S+#', "replace_url_callback", $html)