Bogdan Galan
Bogdan Galan

Reputation: 65

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!

Upvotes: 0

Views: 638

Answers (5)

Bogdan Galan
Bogdan Galan

Reputation: 65

To answer to my "Edit:", I have figured out the problem.

Here is the full function

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

Upvotes: 0

Brian
Brian

Reputation: 8616

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.

Upvotes: 0

Sahil Gulati
Sahil Gulati

Reputation: 15141

Try this hope this simple one will help you out..

Regex: :([^\:]+):

:([^\:]+): 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/$1/" title="$1" target="_blank">$1</a>', $text);
    return $text;
}

Upvotes: 1

Exprator
Exprator

Reputation: 27503

change this /:(\w+):/ to this /:([^:]+):/

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Change the crucial line in the function to the following:

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

Upvotes: 1

Related Questions