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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:20:07+00:00 2026-05-18T11:20:07+00:00

i want to convert text links in my content page into active links using

  • 0

i want to convert text links in my content page into active links using php. i tried every possible script out there, they all fine but the problem that they convert links in img src tag. they convert links everywhere and break the html code.

i find a good script that do what i want exactly but it is in javascript. it is called jquery-linkify.
you can find the script here
http://github.com/maranomynet/linkify/

the trick in the script that it convert text links without breaking the html code. i tried to convert the script into php but failed.

i cant use the script on my website because there is other scripts that has conflict with jquery.

anyone could rewrite this script for php? or at least guide me how?

thanks.

  • 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-18T11:20:07+00:00Added an answer on May 18, 2026 at 11:20 am

    First, parse the text with an HTML parser, with something like DOMDocument::loadHTML. Note that poor HTML can be hard to parse, and depending on the parser, you might get slightly different output in the browser after running such a function.

    PHP’s DOMDocument isn’t very flexible in that regard. You may have better luck by parsing with other tools. But if you are working with valid HTML (and you should try to, if it’s within your control), none of that is a concern.

    After parsing the text, you need to look at the text nodes for links and replace them. Using a regular expression is the simplest way.

    Here’s a sample script that does just that:

    <?php
    function linkify($text)
    {
      $re = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";
      preg_match_all($re, $text, $matches, PREG_OFFSET_CAPTURE);
    
      $matches = $matches[0];
    
      $i = count($matches);
      while ($i--)
      {
        $url = $matches[$i][0];
        if (!preg_match('@^https?://@', $url))
          $url = 'http://'.$url;
    
        $text = substr_replace($text, '<a href="'.$url.'">'.$matches[$i][0].'</a>', $matches[$i][1], strlen($matches[$i][0]));
    
      }
    
      return $text;
    }
    
    $dom = new DOMDocument();
    $dom->loadHTML('<b>stackoverflow.com</b> <a href="stackoverflow.com">test</a>');
    $xpath = new DOMXpath($dom);
    
    foreach ($xpath->query('//text()') as $text)
    {
      $frag = $dom->createDocumentFragment();
      $frag->appendXML(linkify($text->nodeValue));
      $text->parentNode->replaceChild($frag, $text);
    }
    
    echo $dom->saveHTML();
    ?>
    

    I did not come up with that regular expression, and I cannot vouch for its accuracy. I also did not test the script, except for this above case. However, this should be more than enough to get you going.

    Output:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
    <body>
     <b><a href="http://stackoverflow.com">stackoverflow.com</a></b> 
     <a href="stackoverflow.com">test</a>
    </body>
    </html>
    

    Note that saveHTML() adds the surrounding tags. If that’s a problem, you can strip them out with substr().

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

Sidebar

Related Questions

I want to use the Discount C-library to convert Markdown text into HTML. I
Possible Duplicate: Interpreting newlines with XSLT xsl:text? How to convert newline into <br/> with
How can I convert text into link using jQuery and assign a function? I
I want to convert a link to a definite text by using regular expression.While
Possible Duplicates: PHP Regex to convert text before colon to link Return the portion
How can I convert this text file content into a recursive collection of objects
I have some html files that I want to convert to text. I have
I have a TextBoxD1.Text and I want to convert it to an int to
I have a text document (.txt). I want to convert it to an image
I want to convert a primitive to a string, and I tried: myInt.toString(); This

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.