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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:01:00+00:00 2026-06-17T14:01:00+00:00

I’m using the following pattern to capture links, and turn them into HTML friendly

  • 0

I’m using the following pattern to capture links, and turn them into HTML friendly links. I use the following pattern in a preg_replace_callback and for the most part it works.

"#(https?|ftp)://(\S+[^\s.,>)\];'\"!?])#"

But this pattern fails when the text reads like so:

http://mylink.com/page[/b]

At that point it captures the [/b amusing it is part of the link, resulting in this:

<a href="http://woodmill.co.uk[/b">woodmill.co.uk[/b</a>]

I’ve look over the pattern, and used some cheat sheets to try and follow what is happening, but it has foxed me. Can any of you code ninja’s help?

  • 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-17T14:01:01+00:00Added an answer on June 17, 2026 at 2:01 pm

    Ok I solved the problem. Thanks to @Cyborgx37 and @MikeBrant for your help. Here’s the solution.

    Firstly I replaced my regexp pattern with the one that João Castro used in this question: Making a url regex global

    The problem with that pattern is it captured any trailing dots at the end, so in the final section of the pattern I added ^. making the final part look like so [^\s^.]. As I read it, do not match a trailing space or dot.

    This still caused an issue matching bbcode as I mentioned above, so I used preg_replace_callback() and create_function() to filter it out. The final create_function() looks like this:

    create_function('$match','
                    $match[0] = preg_replace("/\[\/?(.*?)\]/", "", $match[0]);
                    $match[0] = preg_replace("/\<\/?(.*?)\>/", "", $match[0]);
                    $m = trim(strtolower($match[0]));
                    $m = str_replace("http://", "", $m);
                    $m = str_replace("https://", "", $m);
                    $m = str_replace("ftp://", "", $m);
                    $m = str_replace("www.", "", $m);
    
                    if (strlen($m) > 25)
                    {
                        $m = substr($m, 0, 25) . "...";
                    }
    
                    return "<a href=\"$match[0]\" target=\"_blank\">$m</a>";
    '), $string);
    

    Tests so far are looking good, so I’m happy it is now solved.

    Thanks again, and I hope this helps someone else 🙂

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.