Search code examples
phphyperlinkwords

Add hyperlinks to words in 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!


Solution

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