0

I'm looking for nice regexp which could change me string from:

text text website.tld text text anotherwebsite.tld/longeraddress text http://maybeanotheradress.tld/file.ext

into bbcodes

text text [url=website.tld]LINK[/url] text text [url=anotherwebsite.tld/longeradress]LINK[/url] text text [url=http://maybeanotheradress.tld/file/ext]LINK[/url]

Could you please advice?

7
  • How are you going to distinguish text from web-site address? Commented Feb 25, 2012 at 15:08
  • First of all I though about splitting strings by delimiters: ":", "-", and a spacebar Commented Feb 25, 2012 at 15:15
  • 1
    @AdrianK. "hi.com whit me" (example of poorly written phrase). Given your current rules, hi.com should be interpreted as an URL. I recommend to force URLs to be prefixed with a protocol. Commented Feb 25, 2012 at 15:20
  • possible duplicate of Find links in page and run it through custom function Commented Feb 25, 2012 at 15:20
  • @ArianK: This has been asked numerous times, even today already. Please use the search, there are different approaches how you could do that, in the end it does not depend whether you insert BBCODE or just a HTML A tag. Commented Feb 25, 2012 at 15:21

1 Answer 1

2

Even I vote for duplicate, a general suggestion: Divide and Conquer.

In your input string, all "URLs" do not contain any spaces. So you can divide the string into the parts that do not contain spaces:

$chunks = explode(' ', $str);

As we know that each part is now potentially a link you can create your own function that is able to tell so:

/**
 * @return bool
 */
function is_text_link($str)
{
    # do whatever you need to do here to tell whether something is
    # a link in your domain or not.

    # for example, taken the links you have in your question:

    $links = array(
        'website.tld', 
        'anotherwebsite.tld/longeraddress', 
        'http://maybeanotheradress.tld/file.ext'
    );

    return in_array($str, $links);
}

The in_array is just an example, you might be looking for regular expression based pattern matching instead. You can edit it later to fit your needs, I leave this as an exercise.

As you can now say what a link is and what not, the only problem left is how to create a BBCode out of a link, that's a fairly simple string operation:

 if (is_link($chunk))
 {
     $chunk = sprintf('[url=%s]LINK[/url]', $chunk);
 }

So technically, all problems have been solved and this needs to be put together:

function bbcode_links($str)
{
    $chunks = explode(' ', $str);
    foreach ($chunks as &$chunk)
    {
        if (is_text_link($chunk))
        {
             $chunk = sprintf('[url=%s]LINK[/url]', $chunk);
        }              
    }
    return implode(' ', $chunks);
}

This already runs with your example string in question (Demo):

$str = 'text text website.tld text text anotherwebsite.tld/longeraddress text http://maybeanotheradress.tld/file.ext';

echo bbcode_links($str);

Output:

text text [url=website.tld]LINK[/url] text text [url=anotherwebsite.tld/longeraddress]LINK[/url] text [url=http://maybeanotheradress.tld/file.ext]LINK[/url]

You then only need to tweak your is_link function to fullfill your needs. Have fun!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.