dongliuzi3410 2017-05-04 11:08
浏览 110
已采纳

在php中添加单词的超链接

I have found this function on buildinternet.com.

function tag_it($text) {
    $text = preg_replace("/:(\w+):/", '<a href="http://www.example.com/page/$1/" title="$1" target="_blank">$1</a>',$text);
    return $text;
}

What it does is adding hyperlinks to words enclosed in ":" and the problem is that it works only with single words and it doesn`t work with words that has a single quote.

So if I do this,

$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:.";
echo tag_it($test);

only to the :word1: will be added a hyperlink, the rest will be ignored.

I don`t know too much php and I would appreciate if someone could make the function work with more than one word and with a single quote too.

Thank you!

Edit:

So I have tried the following to add a different link to words enclosed in "#"

function tag_it($text) {
$out = preg_replace_callback(
    "/:([^\:]+):/",
    function($m) {
        $linkText = $m[1];
        $link = str_replace('"', '', $m[1]);
        $link = str_replace("'", "", $m[1]);
        $link = str_replace(" ", "_", $link);

        return '<a href="http://www.example.com/page/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
    },
    preg_replace_callback(
        "/#([^\#]+)#/",
        function($m1) {
            $linkText1 = $m1[1];
            $link1 = str_replace('"', '', $m1[1]);
            $link1 = str_replace("'", "", $m1[1]);
            $link1 = str_replace(" ", "_", $link1);

            return '<a href="http://www.example.com/blog/'.$link1.'/" title="'.$linkText1.'" target="_blank">'.$linkText1.'</a>';
        },
        $text
    )
);
return $out;
}
$test = "one :word:, #two words#, :this is a phrase:, this has a single quote :don:'t or :don't forget:.";
echo tag_it($test);

and i get this

one word, two_words,_/" title="//www.example.com/blog/two_words/" title="two words" target="_blank">two words, " target="_blank">//www.example.com/blog/two_words/" title="two words" target="_blank">two words, this is a phrase, this has a single quote don't or don't forget:.

It seems to work for the 1st word enclosed in ":" and is trying to work with the words enclosed in "#" too but something is happening along the way and I can`t figure it out.

Any tip is really appreciated.

Thank you!

  • 写回答

5条回答 默认 最新

  • dongwu5743 2017-05-04 11:41
    关注

    I'll let you finish it off, above are all valid, but leave you with broken links, so building on them, I'd do something like:

    function tag_it($text) {
        $out = preg_replace_callback(
        "/:([^:]+):/",
        function($m) {
            $linkText = $m[1];
            $link = str_replace('"', "", $m[1]);
            $link = str_replace("'", "", $m[1]);
            return '<a href="http://www.example.com/page/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
        },
        $text);
        return $out;
    }
    

    You'll need to finish it off to replace spaces with - or _ or something but should be easy enough to work it out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存