简体   繁体   中英

PHP: Regexp to change urls

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM