Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



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

Posts: 1,314
Location: Glasgow, UK
Trades: 0
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?
__________________

Please login or register to view this content. Registration is FREE
- Everything a webmaster needs - for free

Please login or register to view this content. Registration is FREE
- Free web hosts reviewed and rated

Please login or register to view this content. Registration is FREE
- Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-27-2002, 01:12 AM
DaveMo~'s Avatar
Administrator
Defies A Status

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

Dave
__________________

Please login or register to view this content. Registration is FREE
:
Please login or register to view this content. Registration is FREE
:
Please login or register to view this content. Registration is FREE
:

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
?

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

Posts: 258
Trades: 0
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, 09:02 AM
ghettocars's Avatar
Defies a Status

Posts: 1,738
Name: Josh
Location: Miami, FL
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
ghettocars is offline
Reply With Quote
View Public Profile
 
Old 08-28-2002, 06:09 PM
david's Avatar
King Spam Talker

Posts: 1,314
Location: Glasgow, UK
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
- Everything a webmaster needs - for free

Please login or register to view this content. Registration is FREE
- Free web hosts reviewed and rated

Please login or register to view this content. Registration is FREE
- Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile
 
Old 08-28-2002, 08:08 PM
Darkfire001's Avatar
Super Talker

Posts: 124
Trades: 0
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
Please login or register to view this content. Registration is FREE
for the Ultimate Videogame Messageboards

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

Posts: 258
Trades: 0
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, 05:51 PM
david's Avatar
King Spam Talker

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

Any other ideas?
__________________

Please login or register to view this content. Registration is FREE
- Everything a webmaster needs - for free

Please login or register to view this content. Registration is FREE
- Free web hosts reviewed and rated

Please login or register to view this content. Registration is FREE
- Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile
 
Old 09-12-2002, 08:37 PM
Tiggy's Avatar
Ultra Talker

Posts: 249
Location: Lancashire, UK
Trades: 0
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 -
Please login or register to view this content. Registration is FREE
Tiggy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Links In PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.69817 seconds with 12 queries