-3

如何更改字符串示例中的 url:

这是一个链接<a href="http://google.com.au">http://google.com.au</a>

对于这样的事情

这是一个警告页面链接<a href="./warn.php?link=http://google.com.au">http://google.com.au</a>

编辑:

我要做的是获取最终用户输入的描述,他们可能会在描述中输入链接,我想更改所有链接以使它们转到警告页面,警告页面是./warn.php?site=link

字符串可能看起来像这样

This is a awesome description <a href="google.com.au">Google</a> and this another link <a href="http://google.com.au/images">Google images</a>

好的,这是我尝试过的:

$descc = mysql_real_escape_string($_POST['description']);

$descc = preg_replace('"\b(http://\S+)"', '<a href="./warn.php?site='.base64_encode(.'$1'.).'">$1</a>', $descc);
4

3 回答 3

1

检查这个,虽然我不确定你是否真的指的是这个,然后让我知道这个案例——

$mylink = "http://google.com.au";

This a warning page link <a href="./warn.php?link=<?php echo $mylink ?>">http://google.com.au</a>

编辑版本 1.0

即使它在描述框数据上,您也可以通过 jquery 或 php 之类的方式获取它

$mylink = $_GET['desc_name_data']; 

请更具体地解决问题:)

编辑版本 1.1

检查这个然后让我知道——

echo preg_replace('(<a href="http://\S+)', '<a href="./warn.php?site='.'google.com.au'.'">google.com.au', $descc);
于 2012-08-24T11:44:40.093 回答
0

你可以看看preg_replace()

于 2012-08-24T11:43:58.133 回答
0

我不确定我是否理解您的问题,但urlencode可能会有所帮助。

<a href="./warn.php?link=<?php echo urlencode('http://google.com.au'); ?>">http://google.com.au</a>

如果不是这样,那么请更具体地说明您要实现的目标和尝试过的目标。

编辑:

好的,您可以尝试使用HTML 解析器从链接中提取 href,然后适当地重写。这可能比正则表达式更可靠。

如果您将 url 作为查询字符串传递,您仍应添加 urlencode。

于 2012-08-24T11:58:49.890 回答