note 12336 deleted from function.ereg-replace by jimw

From: Date: Fri, 02 Nov 2001 01:56:55 +0000
Subject: note 12336 deleted from function.ereg-replace by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-20209@lists.php.net to get a copy of this message
Replacing URL with <A HREF... (another geek weighs in): Two major challenges: 1) handle weird URLs like Amazon's, etc., 2) extract URL from punctuation (especially dot) that the user might type on the end. My solution: $text = ereg_replace('http://[[:alnum:]]+(\.[[:alnum:]~_-]+)+([/\.][[:alnum:]?&=~_-]+)*/*', '<A HREF="\\0">\\0</A>', $text); Rationale: Find "http://" followed by a single group (like "www") followed by one or more groups that start with a dot and include letters, numbers, tilde, underscore, or dash. (This is the beginning of every address, like "http://www.hubba-hubba.com".) Then, look for zero or more "clauses" that start with slash or dot and include letters, numbers, question mark, ampersand, equal sign, tilde, underscore, or dash. (These are folders, files, session id's, query string parameters, etc. like "/pages/00-34/view.php?id=45&color=red".) Note: you must have dot as a "clause-starter" along with slash, otherwise it will be included in the address if the user types dot at the end of the URL. Finally, look for zero or more slashes (to account for "http://www.what.com/", etc.) Seems to work for me! If you find a valid URL that doesn't work, please mail it so I can fix the pattern.

« previous php.notes (#20209) next »