Php preg replace生成链接

Php preg replace生成链接,php,preg-replace,preg-match,Php,Preg Replace,Preg Match,我正在开发一个简单的cms,在某些情况下,我想在数据库中存储一个段落,其中可能包含一些链接,但我不想在数据库中存储html代码,我希望它如下所示: 一些文本,一些文本| |关键字||http://an-external-site.com/keyword||一些文本和一些文本 然后使用php生成如下链接: $pattern = '=^(.*)<a(.*)href\="?(\S+)"([^>]*)>(.*)</a>(.*)$=msi'; while (preg_matc

我正在开发一个简单的cms,在某些情况下,我想在数据库中存储一个段落,其中可能包含一些链接,但我不想在数据库中存储html代码,我希望它如下所示:

一些文本,一些文本| |关键字||http://an-external-site.com/keyword||一些文本和一些文本

然后使用php生成如下链接:

$pattern = '=^(.*)<a(.*)href\="?(\S+)"([^>]*)>(.*)</a>(.*)$=msi';
while (preg_match($pattern, $row, $txt))
{
  /* $txt[3] holds the link. */
  echo $txt[3]."\n";

  $row = $txt[1]." here was a link ".$txt[6];
}

/* print $row */
print "<br>".nl2br($row);
一些文字,一些文字一些文字和一些文字

那么,怎么做呢?例如,使用preg_replace()

例如,有没有一种方法可以使用预定义的变量将url指向我的站点中的内部页面

一些文本,一些文本| | | | | |关键字| | | |一些文本和一些文本

这将是:

一些文字,一些文字一些文字和一些文字

例如,MS指的是我的站点

因此,这里php函数获取模式,然后决定是像示例1中那样简单地使用完整url,还是用预定义的变量替换MS,该变量等于


谢谢。

我建议您使用一些类来直接在项目中获取非html代码

如果仍要使用regexp,可以使用如下内容:

$pattern = '=^(.*)<a(.*)href\="?(\S+)"([^>]*)>(.*)</a>(.*)$=msi';
while (preg_match($pattern, $row, $txt))
{
  /* $txt[3] holds the link. */
  echo $txt[3]."\n";

  $row = $txt[1]." here was a link ".$txt[6];
}

/* print $row */
print "<br>".nl2br($row);
$pattern='=^(.*)(.*)$=msi';
while(preg_匹配($pattern,$row,$txt))
{
/*$txt[3]保存链接*/
echo$txt[3]。“\n”;
$row=$txt[1]。“这里有一个链接”$txt[6];
}
/*打印$row*/
打印“
”.nl2br($row);
谢谢你的想法,但是它应该如何工作呢?因为你没有提到在函数应该找到类似| | | | | | |的段落中使用的模式||