Jump to content
xisto Community
bthaxor

Need Help For Preg_replace

Recommended Posts

hey trapsters...

firstly: long time no post :) however just because i am no longer Xisto hosted (for your information, i am now a happy customer of Xisto - Web Hosting :)), i still hang around the forums often...

now to my problem: i understand only the basis of php, and after installing the 'wordspew' plugin for wordpress, i have a small php problem.

the wordspew plugin automatically converts http, ftp, https or ftps text in a shout to a link, however it does not seem to convert www. links, which i find weird. the line that converts links is:

$r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\\" title=\"\\\"$target>» $theLink «</a>", $r->text);

all variables have been declared - although a php noob, i believe only the part before '<a href' must be edited, since that is the filter...

could someone please change the code to include 'www' links as well?

thanks for your help.

Share this post


Link to post
Share on other sites

URLs that start with "www" aren't valid absolute links, since they are relative links. If you were to add a ? between (http|ftp)+ and (s), that, if i'm not mistaken, should convert URLs that start with "www" into links; however, the links will be relative. In order to get it to work as expected, you'll have to make your own function that handles the matched results for use with preg_replace_callback() for it to place the protocol (i.e. http://) in the proper place without affecting URLs that already have a protocol prefixed.

Share this post


Link to post
Share on other sites

thank you for your help... but some english might be nice... :)

:):P

would you possibly give me some code? as i said, i know not php...

Since i didn't really understand the logic of the snippet you have provided, i modified the pattern to be (more) logical. Also, since the snippet you've provided contains variables that are relative to the script that it is part of, i removed those variables, therefore mere copying and pasting the code i provide will only cause problems. However, you can still implement it if you analyze the script you took the snippet from and if you can understand what i provide you. :D
Anywho...

<?phpfunction parseUrl($matches){	if (!preg_match("`^https?://|ftps?://`", $matches[0])){		/* assume http:// */		return "<a href=\"http://".$matches[0]."\">»  ".$matches[0]."  «</a>";	} else {		return "<a href=\"".$matches[0]."\">»  ".$matches[0]."  «</a>";	}}$text = "asdf.com;;$test1 = preg_replace_callback("`(?:https?://|ftps?://)?[\w\.\-_]+/?\S*`i", "parseUrl", $text);$test2 = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"$1$2:$3$4\" title=\"\">»  $1$2:$3$4  «</a>", $text);print $test1."<br/>\n";print $test2."\n";?>

Share this post


Link to post
Share on other sites

I tried to understand the code, but that one-liner gave me head-aches.
Nevertheless, I wanted to drop in and say hi, bthaxor! I thought I'll never see you again.


bittr, im sure deep inside you couldnt wait to get rid of me and are aching to get me out of here again :)

truefusion: i have taken a look at your code, seems logical enough (a lot more logical than the other code...), however i am still uncertain for a WORKING solution... yes, my php "skills" do not extend this far...

i will contact the owner of this plugin for help, and will post a solution here for other trapsters that may be having the same problem...

Share this post


Link to post
Share on other sites

i do believe the author of this script has abandoned his forums because he hasnt been online in months... i have tried, despite my lack of php, to first parse http links, then added a line to parse the remaining '; links (without the http) as a url, however this is where my lack of knowledge kicked in... the problem with this, however, is that the 'http://forums.xisto.com/no_longer_exists/' links will then be converted to 'http://forums.xisto.com/no_longer_exists/.' which is not good... i searched the net and came across this... however, can someone please help me incorporate this into the code given above? a line which checks whether there's a . before the www, then and only then converts the www to a link... thanks.

Share this post


Link to post
Share on other sites

So I took the function from the link you provided, and compressed it into a single line. Because single line things are cool!

<?php function linkify($text){return eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>',eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="\\1">\\1</a>',((substr($text,0,4)=="http://forums.xisto.com/no_longer_exists/;;}'>http://forums.xisto.com/no_longer_exists/;;} echo linkify("you@example.com"); echo "<br />";echo linkify("
https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=
https://mail.google.com/mail/
you can do stuff like this in gmailecho "<br />";echo linkify("http://forums.xisto.com/no_longer_exists/ "<br />";echo linkify("ftp://ftp.example.com");echo "<br />";echo linkify("http://forums.xisto.com/no_longer_exists/;

Edited by Nabb (see edit history)

Share this post


Link to post
Share on other sites

The Tutorial you link to above works fine IF the www is preceded by a "space".
It fails without the space in front of the www.

// WWW example// note the space in front of the www// won't work without the space$text = " example.com;;echo makeClickableLinks($text);

Also, I believe the code above by nabb actually runs trough the php parser more quickly than the eregi_replace used in the Tutorial, so use the code provided by Nabb would be my recommendation.

Share this post


Link to post
Share on other sites

ahh, but here's my problem: this all has to be applied to user input in a shoutbox, so i have no idea how to apply the format nabb has given me...

here, once again, is the original line from the wordspew plugin:

$r->text = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\\" title=\"\\\"$target>» $theLink «</a>", $r->text);

the $theLink variable has already been declared - i just have no idea how to slip the 'www' into there...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.