php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #21869 Link and Mailto parsing functions
Submitted: 2003-01-24 16:11 UTC Modified: 2003-01-24 16:48 UTC
From: spoon at nexdot dot net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5CVS-2003-01-24 (dev) OS: XP Pro
Private report: No CVE-ID: None
 [2003-01-24 16:11 UTC] spoon at nexdot dot net
First, this task can be accomplished with a regex, but it is only _so_ effective.

If there was some way to make functions that would take in a string, and return a string that has all links, and all email addresses (2 seperate functions, or one with a set of switches) properly formatted to work as links.

The reason it is hard with regex, is becuase it is impossible to search for a link when it can be formatted so many ways.



/********** simple **************/
$string = "Hey, check out http://www.spoonified.com, and email me at spoon@nexdot.net";

$linkified = hrefparse($string);

echo $linkified;
/*******************************/

returns this:

Hey, check out <a href="http://www.spoonified.com">http://www.spoonified.com</a>, and email me at <a href="mailto:spoon@nexdot.net">spoon@nexdot.net</a>



of course, the link, the main reason regex is bad, could have many things in it, it could point to a file, have a query string in it, have no www, have many PERIODS (multiple subdomains), be https://, and also not be seperated from other text (such as "OMFGwww.spoonified.comRULES"), and the list goes on.

Email addresses follow the same conditions i mentioned above (minus a few of course, and plus a few also).

Maybe im just overlooking a complex enough regex to do this.

I DO know it can be done. Many IRC/Instant Message programs do a wonderful job of it. They take a link, no matter what conditions, or an email address, and will show an active link once sent. Obviously there is a rexeg im too lame to understand, or a better way to accomplish it.



(also, you 5.0.xCVS is still having an error on the php4apache2.dll compile, just a sidenote, no comments needed, i think you guys are already aware)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-24 16:13 UTC] spoon at nexdot dot net
THIS PAGE DOES IT! (NOT THE EMAIL THOUGH) (but it is a non full proof regex?)

Still, if it is a GOOD regex, that almost never messes up, a function would be ... easier :D
 [2003-01-24 16:21 UTC] tal@php.net
Dup of #6893.
 [2003-01-24 16:32 UTC] spoon at nexdot dot net
Pear does not have this ability, as far as i can see from pear.php.net
 [2003-01-24 16:45 UTC] spoon at nexdot dot net
In the source of your bug.php, at the very bottom it has a line:

$note = addlinks(preg_replace("/(\r?\n){3,}/","\n\n",wordwrap($comment,72,"\n",1)));


addlinks is not defined in the source, so i am assuming it is in prepend.inc (?), but unfortunatley, we cannot view .inc files using source.php, any help?
 [2003-01-24 16:48 UTC] spoon at nexdot dot net
/include/functions.inc  (i was forgetting /include)

function addlinks($text) 
{
    $text = htmlspecialchars($text);
    $text = preg_replace("/((mailto|http|ftp|nntp|news):.+?)(&gt;|\\s|\\)|\\.\\s|$)/i","<a href=\"\\1\">\\1</a>\\3",$text);
    # what the heck is this for?
    $text = preg_replace("/[.,]?-=-\"/", '"', $text);
    return $text;
}


WOOOO. PHP.NET RULES!
 [2003-01-24 16:48 UTC] spoon at nexdot dot net
closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC