Jump to content

Recommended Posts

Ok, I\'ve got a text in the DB, and in that text, is the text :


http://www.xzazx.com

Now, when I view it in the browser, it just displays this as an text,

and I want it to be a link. How can i do this plz?

 

here\'s what i mean :

http://www.xzazx.com/forum/index.php?fid=1...c&id=19&start=0

 

thx!

Link to comment
https://forums.phpfreaks.com/topic/101-link-from-mysql/
Share on other sites

IF it is only urls in the text it is pretty easy to add some <a href=.... stuff...

 

If the links are in a block of text like:

 


For more information please go to [url]http://palle.ninja.dk[/url]

 

For more information please go to http://palle.ninja.dk

 

 

You probably need to do a really fancy regular expression to catch them all and make no mistakes OR do like this site and add some tags to the text (like the URL /URL tags used above).

 

I have a quick cheat sheet for reg.expression on the folowing url

http://www.daimi.au.dk/~biopv/public/reg.exp.php

 

it\'s not only for php, so if you\'re stuck, look around...

 

But my guess would be something like

 

/.*(http://[^s].[^s]).*/

 

Matches http://anything.anything stop at spacecharacter.. But needs

at least one dot in the webaddress...

 

You then need to make the correct replace string, which is not so easy, look around - you\'re probably not the first facing this problem...

 

P.

 

 

 

P.

Link to comment
https://forums.phpfreaks.com/topic/101-link-from-mysql/#findComment-292
Share on other sites

Try this function (not mine, just found it at http://www.bjclark.com/code/linkmail.phps

)

 

 


function show_links($text)

{

  $ret = eregi_replace( "([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", 

      "<a href="\\1://\\2\\3">\\1://\\2\\3</a>", $text);

  $ret = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", 

      "<a href="mailto:\\1">\\1</a>", $ret);

  return($ret);

}

 

i.e. use

echo show_links($text);

 

P.

Link to comment
https://forums.phpfreaks.com/topic/101-link-from-mysql/#findComment-294
Share on other sites

Hi Guys,

 

I agree to Aveach\'s solution, but the problem is that if the text is stored like:

 

$text = \"Welcome to our site, please visit http://www.biodyk.dk for information about our scubadiving or mail me palle@biodyk.nospam.dk\";

 

And you want to find adresses like the biodyk.dk address above, highlight and format it correctly...

 

And it gets worse, what is people don\'t write the http:// or the mailto: ? You could be really evil, parse all places with www.word.* until a space and words with a @ in them - let php check if it exists and if it does insert the formatting. (I don\'t think you can get php to check if a mail address is valid though.

 

P.

Link to comment
https://forums.phpfreaks.com/topic/101-link-from-mysql/#findComment-305
Share on other sites

Ok, here\'s a function which returns all kinds of adresses:




function showlinks($text){

// match any " word://word.word "

 $text = eregi_replace( "([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href="\\1://\\2\\3">\\1://\\2\\3</a>", $text);

// match any " word.word " no @ or //: allowed

 $text = eregi_replace( "[[:space:]]([^[:space:]|//:|@]*)(.[^[:space:]]*)([[:alnum:]#?/&=])[[:space:]]", " <a href="http://\\1\\2\\3">\\1\\2\\3</a>", $text);

// match email adresses

  $text = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href="mailto:\\1">\\1</a>", $text);

return $text;

}



$text = "Welcome to our site, please visit http://www.biodyk.dk for information about our scubadiving or mail me palle@biodyk.nospam.dk or try www.biodyk.dk <p>";

echo $text;

echo showlinks($text);

 

P.

Link to comment
https://forums.phpfreaks.com/topic/101-link-from-mysql/#findComment-307
Share on other sites

  • 7 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.