Jump to content

Displaying URL as a link!


justcrapx

Recommended Posts

Ah, is my english so bad?

I know how to echo query results and how to use anchor tags.
What i really cant figure out is how to extract urls from a complete text.

Just pretend theres a database entry like: "hello dude, check this website. www.phpfreaks. com"

when i fetch this entry inside anchor tags it becomes something like,

<a href="hello dude, check this website. www.phpfreaks.com">

but i want the following output.

<p>hello dude, check this website.</p><a href="www.phpfreaks.com">www.phpfreaks.com</a>

Well i hope this time i could explain the matter.
Link to comment
Share on other sites

Ah ok, I see what you mean
regular expressions, you would have to use regular expressions to hunt down the link, and change it to an anchor, have it place a <a href=" before it, and then have hte link, then have it put a " title="if you want one">name here</a>
you have to have it insert all of that where you want using string replace, and search and find fucntions, comparing strings, you are tlaking about working with regular expressions, above what I know about regular expressions, but that is going tobe where you want to look.
Link to comment
Share on other sites

His should work with everything, if not, then just expand it a little.

If you are having problems.
Integrate what he showed you into your code, test it out, see where it work's, adn where it doesn't see what you can do, then bring it back here, post the entire code again, and tell us what it does, what it doesn't do, and what you need help with.
Then you can go from there.
Link to comment
Share on other sites

The string is always a message just like each one in this forum topic. So i dont have the chance to know the scheme, domain name or path everytime. What i needed was some code to fetch any link inside the string between anchor tags and to  target the link itself. Finally i managed to find a way. That works for any probable url for the http:// or www scheme.

[code]
<?php

$mssage = preg_replace('=([^\s]*)(www\.)=', ' http://www.', $mssage);
$mssage = preg_replace('=([^\s]*)(\w://[www\.]*)([^\s]*)=', '<a href="\\1\\2\\3\\4" target=_blank>\\1\\2\\3\\4</a>', $mssage);

return $mssage;

?>
[/code]
Link to comment
Share on other sites

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.