Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2010, 12:19 PM   PM User | #1
Deacon Frost
Regular Coder

 
Deacon Frost's Avatar
 
Join Date: Feb 2008
Location: Between the Lines
Posts: 279
Thanks: 31
Thanked 4 Times in 4 Posts
Deacon Frost is on a distinguished road
Hyperlink URLS

I've got this function:

PHP Code:
/* clickable($url) changes a block of text to use urls */
function clickable($url){
 
$in = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si');
  
$out = array('<a href="$1" rel="nofollow" target="_blank">$1</a>''<a href="http://$1" rel="nofollow" target="_blank">$1</a>');
   return 
preg_replace($in,$out,$url);

And, I just tried it out with a series of other functions, and it went haywire. I don't understand regex yet, so help is appreciated ^.^.

Here's the context I'm using it in:

PHP Code:
/* escape_data($string) This function cleans data for databases. */

function escape_data ($data) {
db_connect();
 if (
ini_get('magic_quotes_gpc')) {
  
$data stripslashes($data);
 }
  
$data mysql_escape_string (trim($data));
db_close();
   return 
$data;
}

/* format_string($str) fixes a string for input and display */
function format_string($str) {
 
$string escape_data($str);
  
$string str_replace("\n""<br />"$string);
   
$string clickable($string);
     return 
$string;
}

echo 
format_string($input); 

The problem I was having was rather odd. The hyperlink linked multiple url strings with 1 anchor. But it did that twice. The format_string function also failed to replace '\n' with '<br />' in the instance when I used clickable(). It's never done that before, though =/.
Deacon Frost is offline   Reply With Quote
Old 01-09-2010, 02:55 PM   PM User | #2
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
If you're working with URL's, I highly recommend using parse_url() instead of a regex
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:11 AM.


Advertisement
Log in to turn off these ads.