Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:29:48+00:00 2026-05-30T13:29:48+00:00

I’m looking for nice regexp which could change me string from: text text website.tld

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:29:50+00:00Added an answer on May 30, 2026 at 1:29 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.