【发布时间】:2012-02-10 12:13:17
【问题描述】:
我有一个带有一些 html 内容和一些 href 链接的 php 变量。我需要捕获此链接,保存到数据库并将其替换为我刚刚保存的行的 id(用于跟踪有多少人在时事通讯应用程序中关注该链接)。
基本上我需要执行示例中的 2 个函数(some_function_to_save_the_links_to_array 和 some_function_to_save_the_links_to_array)。
非常感谢您的帮助!
例子:
$var = "<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.google.com'>Some links</a> in the body of this <a href='http://www.yahoo.com'>Newsletter</a> and I want to extract their.</body></html>";
//Here I just no know how to do this, but I need to save http://www.google.com and http://www.yahoo.com to, maybe, an array, and save this array in a mysql db.
some_function_to_save_the_links_to_array;
while (THERE ARE VALUES IN THE ARRAY OF THE LINKS){
save $array['X'] to db //(I already know how to do it, this is not the problem)
$id = last inserted row in db //I know how to do it also
function_to_replace_the_links_for_the_id;
}
echo $var;
And this is the echo:
<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.mysite.com/link.php?id=1'>Some links</a> in the body of this <a href='http://www.mysite.com/link.php?id=1'>Newsletter</a> and I want to extract their.
【问题讨论】:
-
删除了我的答案,Gordon提供的链接中的例子更胜一筹。
标签: php hyperlink html-parsing