为 php 中的单词添加超链接

Add hyperlinks to words in php

我在 buildinternet.com 上找到了这个功能。

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

它所做的是将 hyperlinks 添加到包含在“:”中的单词,问题是它仅适用于单个单词,不适用于具有单引号的单词。

所以如果我这样做,

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

只有:word1:会被加一个hyperlink,其余的将被忽略。

我知道的不多php,如果有人可以使该函数在多个单词和单引号下工作,我将不胜感激。

谢谢!

编辑:

所以我尝试了以下方法来为包含在“#”中的单词添加一个不同的 link

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);

我明白了

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:.

它似乎适用于包含在“:”中的第一个单词,并且也在尝试处理包含在“#”中的单词,但过程中发生了一些事情,我无法弄清楚。

非常感谢任何提示。

谢谢!

将函数中的关键行更改为以下内容:

$text = preg_replace("/:([^:]+):/", '<a href="http://www.example.com/page//" title="" target="_blank"></a>', $text);

将此 /:(\w+):/ 更改为此 /:([^:]+):/

试试这个,希望这个简单的方法能帮到你..

正则表达式: :([^\:]+):

:([^\:]+): it will match : then all till not : and then : in end

Try this code snippet here

<?php
ini_set('display_errors', 1);
$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:.";
echo tag_it($test);
function tag_it($text)
{
    $text = preg_replace("/:([^\:]+):/", '<a href="http://www.example.com/page//" title="" target="_blank"></a>', $text);
    return $text;
}

我会让你完成它,以上都是有效的,但给你留下了损坏的链接,所以在它们的基础上,我会做类似的事情:

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;
}

你需要完成它以用 - 或 _ 或其他东西替换空格,但应该很容易解决。

为了回答我的“编辑:”,我已经找到问题了。

这里是完整的函数

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>';
        },$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/blog/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
        },$out
    );

    $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/article/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
        },$out
    );

    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);