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 7783253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:46:58+00:00 2026-06-01T19:46:58+00:00

im making a comment section on a website. At first I needed to do

  • 0

im making a comment section on a website.
At first I needed to do a regular expression that finds any url and replace it surrounded with

<a href="url"></a>  

So I found a super regular expression to find all the url’s in a comment and I did a function that returns all the urls with the html tag:

function addURLTags($string) {
    $pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
    return preg_replace($pattern, '<a href="$1">$1</a>', $string);
}

Everything went excellent. but one week ago my boss told me that now I have to add bbcode to the comment section. And I was like “no problem”… but then he told me that my function addURLTags has to stay.

So any string like this:

http://www.google.com
[url]http://www.google.com[/url]
[url="http://www.google.com"]http://www.google.com[/url]

must be replaced to the same string:

<a href="http://www.google.com">http://www.google.com</a>

So I got a little php library that replaces all bbcode ocurrences to html code.

And I thought: “Ok, first I should get all url ocurrences that do not have a [url] tag in the beggining! And second I replace all the bbcode tags”

And I tried to add a negative assertion at the beggining of the super regex, something like this:

/(?i)\b((?![url])(?:https?://|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|(([^\s()<>]+|(([^\s()<>]+)))))+(?:(([^\s()<>]+|(([^\s()<>]+))))|[^\s`!()[]{};:’\”.,<>?«»“”‘’]))/

but didnt work!

Im kinda newbie with regular expressions and all the online testers I tried do not work well with such a long regex. I dont know what else try.

Do you have any suggestion? Do you know any PHP lybrary that does the “url replacing” with and without the [url] bbcode tags?

Thank you in advance.

  • 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-06-01T19:47:00+00:00Added an answer on June 1, 2026 at 7:47 pm

    You solve two problems here. So solve them separately and don’t quench everything into a single regular expression which is more or less making things more complicated instead of less.

    Divide and Conquer:

    First use your bbcode library to locate the parts where those urls are, so that you can create a stream of your text:

    "normal text", "bbcode", "normal text", "bbcode"
    

    Then you apply the bbcode library to create the URLs only on the “bbcode” segments, and your URL clickable-maker will be applied to the “normal text” segments only.

    After all segments have been processed, you concatenate all back into one string.

    Voila, problem solved.

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

Sidebar

Related Questions

Making websites that appear correctly in IE is a big problem. Is there any
I am making an ajax comment system for my website and I am querying
I am making an inline comment reply system It's mostly working except that I
I'm making a comment box which allows for nested comments and is basically structured
Making an adobe flex ui in which data that is calculated must use proprietary
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
While making some final tests of a class-library that I'm writing for Windows Mobile
I'm making a site, and will be allowing guests to vote/comment. How should I
I'm making a image website with a bunch of images on the website, and

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.