0

I have the following php code:

if(isset($_POST['text'])) { $text= mysql_real_escape_string($_POST['text']); } 

where $text is utf-8. What I am trying to do is to parse the text as so to find if there are any URLs within $text and tag them accordingly. For example, if $text was to have:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris laoreet congue luctus. In fringilla mattis nisl. Vestibulum id eros sed ante vestibulum faucibus. Vestibulum luctus enim sit amet ante imperdiet placerat. Fusce imperdiet rhoncus tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ullamcorper metus vel diam consectetur, vel laoreet lectus www.google.com elementum. In ut nunc id felis bibendum viverra nec quis dui. Donec non purus commodo, vestibulum nisl non, lobortis eros. Ut et arcu convallis, posuere nibh a, venenatis ligula. Mauris ullamcorper massa libero, placerat pretium magna condimentum ut. Ut elit turpis, imperdiet pretium mauris vitae, iaculis facilisis quam. Maecenas ut vulputate libero.

Then I'would like it to appear this way:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris laoreet congue luctus. In fringilla mattis nisl. Vestibulum id eros sed ante vestibulum faucibus. Vestibulum luctus enim sit amet ante imperdiet placerat. Fusce imperdiet rhoncus tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ullamcorper metus vel diam consectetur, vel laoreet lectus www.google.com elementum. In ut nunc id felis bibendum viverra nec quis dui. Donec non purus commodo, vestibulum nisl non, lobortis eros. Ut et arcu convallis, posuere nibh a, venenatis ligula. Mauris ullamcorper massa libero, placerat pretium magna condimentum ut. Ut elit turpis, imperdiet pretium mauris vitae, iaculis facilisis quam. Maecenas ut vulputate libero.

My doubt consists in whether I should iterate through each character and try to detect an URL or run the $text through a regex (with php?) and attempt the same. In this last case (of the regex) how am I supposed to find the index as so to create an tag? Thank you for your help.

5
  • 1
    Obligatory please don't use mysql_ functions as they are depreciated. Commented Dec 7, 2013 at 15:11
  • lol I actually wasn't aware of this. Thank you very much for this precious information! Commented Dec 7, 2013 at 15:14
  • Also check this page out it should answer your question: Find URLs in Text, Make Links Commented Dec 7, 2013 at 15:17
  • it is indeed a duplicate @popnoodles, sorry and thank you for pointing that out :) Commented Dec 7, 2013 at 15:35
  • @user3050963 ok, it's very commonplace Commented Dec 7, 2013 at 15:37

1 Answer 1

2

I found an answer fairly quickly from another stackoverflow question with some quick research:

return preg_replace('!(http://[a-z0-9_./?=&-]+)!i', '<a href="$1">$1</a> ', $text." "); 

Reference: https://stackoverflow.com/a/5252897/2827066

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.