Reply
Links In PHP
Old 08-24-2002, 03:17 PM Links In PHP
david's Avatar
King Spam Talker

Posts: 1,314
Location: Glasgow, UK
For part of my site I wanted to format plain text from a database so that URLs would appear as links in HTML. I am using the following code:
PHP Code:
function link_urls($string) {
  
//change single to double quotes (only necessary if you work with databases)
  
$string ereg_replace("\'""\""$string);
  
//change full stops and question marks that don't need to be linked
  
$string ereg_replace("\? "" question_mark "$string);
  
$string ereg_replace("\?\r\n"," question_mark_new_line "$string);
  
$string ereg_replace("\. "" full_stop "$string);
  
$string ereg_replace("\.\r\n"," full_stop_new_line "$string);
  
$string eregi_replace("(([url]ftp://[/url])|([url]http://[/url])|(www))(([a-z0-9.\-])*)(@)?(([a-z0-9\.])*)(([:/])?)((([a-z0-9]*)(@?)/*)+([.a-z]{2,4}){1})?((\??[a-z0-9\=]*)?)((\&[a-z0-9\=]*)?)(([\.a-z]){2,4})?""<a href=\"\\0\">\\0</a>"$string);
  
//change back dots and question marks
  
$string ereg_replace(" full_stop_new_line ""\.\r\n"$string);
  
$string ereg_replace(" full_stop""\."$string);
  
$string ereg_replace(" question_mark_new_line ""\?\r\n"$string);
  
$string ereg_replace(" question_mark""\?"$string);
  return 
$string;

but I have a problem that it can't cope with dashes in URLs (see

this page for an example.

As I didn't write that code above (and don't really understand how it works) I can't edit it to overcome the problem.

Does anyone know what should be changed and how?
__________________
Free Webmaster Help - Everything a webmaster needs - for free
Free-Webhosting.info - Free web hosts reviewed and rated
Web Hosting Hunt - Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile Visit david's homepage!
 
When You Register, These Ads Go Away!
Old 08-27-2002, 12:12 AM
DaveMo~'s Avatar
Administrator
Defies A Status

Posts: 10,209
Name: Dave
Location: Scott Depot, West Virginia, USA
No help with this one yet? Where's all our PHP programmers?

Dave
DaveMo~ is offline
Reply With Quote
View Public Profile Visit DaveMo~'s homepage!
 
Old 08-27-2002, 02:35 AM
AhmedF's Avatar
Left for Better Places

Posts: 258
Sigh

So many regular exprssions its hurting my brain.

Can you tell me EXACTLY What you want it do [as I got confused] and Ill whip something up NP
AhmedF is offline
Reply With Quote
View Public Profile
 
Old 08-27-2002, 08:02 AM
ghettocars's Avatar
Defies a Status

Posts: 1,736
Name: Josh
Location: Miami, FL
lol i would help you if i knew more PHP
then again david taught me PHP, so really the student cant help the master
sorry
__________________
www.goatslacker.com
ghettocars is offline
Reply With Quote
View Public Profile
 
Old 08-28-2002, 05:09 PM
david's Avatar
King Spam Talker

Posts: 1,314
Location: Glasgow, UK
Thanks for the response AhmedF.

Basically the idea is that I can present my user-entered content better. Users enter the plain text of their questions, which often include links. What I am trying to do is, when I output the data from the database, format the link in the browser so that it is clickable.

That code (which I got from PHPBuilder I think) works for the majority of links, e.g.:
http://www.mysite.com
http://www.mysite.com/apage/
http://www.mysite.com/something/page.htm

but for some reason can't cope with:
http://www.mysite.com/something-else/page.htm
because it stops the link at -

I'd really appreciate it if you could edit that code slightly or make some to do that
__________________
Free Webmaster Help - Everything a webmaster needs - for free
Free-Webhosting.info - Free web hosts reviewed and rated
Web Hosting Hunt - Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile Visit david's homepage!
 
Old 08-28-2002, 07:08 PM
Darkfire001's Avatar
Super Talker

Posts: 124
I believe that there is some way to skirt past that, since it brings up Quote symbols and such in my PHP Guide but I bet you people have already thought of that.
__________________
******************************
Okay I have finally given into the Darkside, I will now start including my sig on this site.
******************************

Visit www.gamerpub.com for the Ultimate Videogame Messageboards

******************************
^ LOL Whats with the Asteriks?
Darkfire001 is offline
Reply With Quote
View Public Profile Visit Darkfire001's homepage!
 
Old 08-29-2002, 01:45 AM
AhmedF's Avatar
Left for Better Places

Posts: 258
Yech

I suck with regular exprssions.

BUT ...

try changing '[a-z0-9]'
to :alpha: or :alphanum:

Basically .. teh script is looking for letters a to z, and numbers 0 to 9 ... the '-' is neither ... so it chokes. By telling to to look for 'alphanumeric' instead, it should be able to parse the '-'

If this doesnt work, we can write a bit simpler, and maybe not as fact strpos and str_replace replacement
AhmedF is offline
Reply With Quote
View Public Profile
 
Old 09-01-2002, 04:51 PM
david's Avatar
King Spam Talker

Posts: 1,314
Location: Glasgow, UK
Thanks for thetip AhmedF, but unfortunately it didn't seem to fix the problem

Any other ideas?
__________________
Free Webmaster Help - Everything a webmaster needs - for free
Free-Webhosting.info - Free web hosts reviewed and rated
Web Hosting Hunt - Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile Visit david's homepage!
 
Old 09-12-2002, 07:37 PM
Tiggy's Avatar
Ultra Talker

Posts: 250
Location: Lancashire, UK
Try changing [a-z0-9] to [a-z0-9.-_~] you should be able to put in any new characters to the expression at this point... I see things link [a-z0-9.-] in there, expand the brackets with new characters (at the end, so it doesn't interfere with your ranges)

Should do the trick, not that I know PHP, but I've done a few regex in my time.
__________________
Web Site Owner's Resource - http://websiteowner.info
Tiggy is offline
Reply With Quote
View Public Profile Visit Tiggy's homepage!
 
Reply     « Reply to Links In PHP
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.17959 seconds with 12 queries