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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:11:14+00:00 2026-06-13T16:11:14+00:00

I seem to be having an issue replacing a text link with a link

  • 0

I seem to be having an issue replacing a text link with a link to the site posted, it isn’t linking.

The code:

$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text);
echo $status_text;

$status_text is pulled from a MySQL field named contents, and contains other text, but i’d just like to linkify the link. Additionally, i’d also like it to not display the full URL, just the main domain.

UPDATE: We have two other preg_replaces on the same page, looking for things with + and # in front of them that link to areas of the site, they currently work and need to not conflict with the above:

$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text);


$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $status_text);
  • 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-13T16:11:15+00:00Added an answer on June 13, 2026 at 4:11 pm

    Here, try this:

    $status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
    echo $status_text;
    

    or to make it a little easer to read:

    $m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
    $r = '$1 <a href="$2">$3</a>';
    
    $status_text = preg_replace($m,$r,$status_text);
    echo $status_text;
    

    EDIT — Updating due to new info in the OP —

    Your hashtag regex does not take into account hashes in URLs, so let’s fix that…
    Also, you should match for URLs before matching for hash-tags or plus-tags because otherwise you will mess up the links you create for the hash-tags and plus-tags

    $status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
    $status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    $status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    

    Or to make it a little easier to read…

    $match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
    $match_hash = '|\B#([\d\w_]+)|i';
    $match_plus = '|\B\+([\d\w_]+)|i';
    $replace_url = '<a href="$1">$2</a>';
    $replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';
    
    $status_text = preg_replace($match_href, $replace_url, $status_text);
    $status_text = preg_replace($match_hash, $replace_tag, $status_text);
    $status_text = preg_replace($match_plus, $replace_tag, $status_text);
    

    EDIT AGAIN — Adding a URL that might be helpful —

    You can test out regular expressions here: http://gskinner.com/RegExr/

    ANOTHER EDIT

    As per a comment/question, if you want to account for urls that lack the http protocol, use the following:

    $status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
    $status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    $status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
    

    Or to make it a little easier to read…

    $match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
    $match_hash = '|\B#([\d\w_]+)|i';
    $match_plus = '|\B\+([\d\w_]+)|i';
    $replace_url = '<a href="$1">$3</a>';
    $replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';
    
    $status_text = preg_replace($match_href, $replace_url, $status_text);
    $status_text = preg_replace($match_hash, $replace_tag, $status_text);
    $status_text = preg_replace($match_plus, $replace_tag, $status_text);
    

    EXAMPLE USAGE

    Input: (Text from DB -> $status_text)

    <!-- language-all: lang-none -->
    Hi, this is an example. This is a url http://stackoverflow.com/ 
    and this is a hash reference that we want to link to an internal 
    post #AwesomePost123 and this one is a plus reference we want to 
    link to an internal post +AwesomePost123 and finally an example 
    of a url without the http protocol www.stackoverflow.com
    

    Output: (After running through the regex)

    <!-- language-all: lang-none -->
    Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a> 
    and this is a hash reference that we want to link to an internal 
    post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to 
    link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example 
    of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue that I can't seem to figure out. Hopefully somebody
I am having an xslt issue that I cannot seem to solve. Right now
I am having a strange issue that I can't seem to resolve. Here is
I seem to be having trouble with my code. I need to say: if
I seem to be having an issue with SSL whenever trying to use oAuth2
I seem to be having an issue with Jax-WS and Jax-b playing nicely together.
I seem to be having an issue with Xalan's translate method. I have the
Seem to be having an issue with the tablesorter plugin and excluding columns for
I seem to be having an issue with the conditional compilation tags directly en
I seem to be having an issue with Java and NetBeans when it comes

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.