Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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 将字符串url扭曲为html链接标记_Php_Mysql - Fatal编程技术网

Php 将字符串url扭曲为html链接标记

Php 将字符串url扭曲为html链接标记,php,mysql,Php,Mysql,所以我尝试将url(如www.google.com)替换为html链接标记,如下所示 return preg_replace('"\b(www.\S+)"', '<a href="$1" style="color:white;">$1</a>', $url); 那么它也将取代 http://www. 也。。。。 如果我当前的配置是just replace www.但如果我键入http://则它将仅从www.google.com扭曲并离开http:// 编辑: 所以我想

所以我尝试将url(如www.google.com)替换为html链接标记,如下所示

return preg_replace('"\b(www.\S+)"', '<a href="$1" style="color:white;">$1</a>', $url);
那么它也将取代

http://www.
也。。。。 如果我当前的配置是just replace www.但如果我键入http://则它将仅从www.google.com扭曲并离开http://

编辑: 所以我想要一个flexibel标签,所以如果我输入www.google.com或者它会捕获它并将其转换为链接标签。。。。 而且如果我输入like,它只会显示like

<a href"https://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>

第二次编辑:对不起,我忘了说我还想在链接中保留其他文本。。所以,这就像我在键入“www.xxxxx.com中有一条大新闻”,当我显示它时,它将是“xxxxx中有一条大新闻”,其中xxxxx将是可点击的链接。。。 我之所以这么做是因为我要用它来做我的网站发布部分

我们可以这样做吗

更新:

我想了一些主意,首先我们做preg_replace来捕获http://并将其删除,然后我们捕获www。将其扭曲成html链接标记,并捕获www.和afer.com/.co/或任何内容,并将其删除到下一个空格

但是我不知道如何使用preg_replace,因为我不知道它是如何工作的

你是说这个

 $text = ' This is a text with the link of google.com. And another phrase with google.com url.';
 echo str_replace('google.com','<a href="http://google.com" target=_blank>google</a>',$text);
$text=”这是一个带有google.com链接的文本。还有另一个带有google.com网址的短语;
echo str_替换('google.com',''$text);

为了实现您的目标,这是我能想到的最接近的方法

我相信还有其他的方法,但是你可以尝试下面的方法

文本文件中可以包含以下内容:(可以有1行或多行)

bookmark.txt(以所示的精确格式)-您不需要将
http://
自动放入

旁注:您可以使用
google.com
www.google.com
-只有
google
这个词会显示为链接,但完整的
href
会显示在HTML源代码中,并且是一个有效的URL

Google google.com Yahoo yahoo.com stackoverflow stackoverflow.com/posts/26154124 您是否应该决定使用类似于
谷歌搜索

即:

谷歌搜索| Google.com 雅虎| Yahoo.com stackoverflow链接| stackoverflow.com/posts/26154124
您也可以尝试:

$content=”http://stackoverflow.com/posts/26154124";
$content=preg_replace('$(https?:/[a-z0-9./?=&#-]+)(?![^]*>)$i','','$content'';
echo$内容;


但是在这一点上,我一遍又一遍地阅读你的问题,包括你的编辑,我不完全确定你想要的是什么,我认为这是非常广泛的,超出了我对PHP的知识范围。

你总是对的,但只是犯了一个简单的打字错误

<a href"http://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>

在href之后,您错过了“=”等号。请试试这个

<a href="http://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>


希望它有帮助…

您的意思是
我们需要查看您使用的
$url
作为。是“谷歌”吗?是“www.google.com”吗?其他?我想要flexibel,所以如果$url=www.google.com或$url=它只会给出与>google相同的输出,看看我下面发布的内容。这是一种方法。www.google.com是唯一的例子。。。我希望它能捕获任何url,不仅仅是www.google.com谢谢你的努力。。。。我尝试了第二种方法,它只适用于http://和https://并且如果我从www开始,它无法捕获链接。而且链接标记扭曲了预定义的文本,即stackoverflow,如何根据捕获的文本进行更改。。。对于第一个,我还没有尝试,但从我看来,它似乎是唯一的重点文本,希望变成链接。。。。好吧,这也是我的错,我忘了解释我不仅提供链接输入,而且还提供其他文本…@PUCUK,不客气。我不能完全肯定这会解决你的问题。我试过了,谢谢,抱歉,只是在这个问题上打错了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 Google google.com Yahoo yahoo.com stackoverflow stackoverflow.com/posts/26154124
<?php 
$file = "bookmark.txt";
$lines = file($file);
foreach($lines as $line)
{
    $piece=explode(" ",$line);
    $link=trim($piece[1]);
    $text=trim($piece[0]);
    echo '<a href="http://'.$link.'">'.$text.'</a><br>';
}
Google|google.com Yahoo|yahoo.com stackoverflow|stackoverflow.com/posts/26154124 Google Search|google.com Yahoo|yahoo.com stackoverflow link|stackoverflow.com/posts/26154124
$content = "http://stackoverflow.com/posts/26154124";
$content = preg_replace('$(https?://[a-z0-9_./?=&#-]+)(?![^<>]*>)$i', ' <a href="$1" target="_blank">StackOverflow</a> ', $content." ");

echo $content;
<a href"http://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>
<a href="http://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>