List Info

Thread: note 78359 added to function.preg-replace




note 78359 added to function.preg-replace
user name
2007-10-08 09:18:23
Here's a derivate of Iasmin's function.
This one handles URL's followed by a period or comma too and
adds a _blank target to the hyperlinks.
Additionally it replaces e-mail addresses with a mailto
hyperlink.

<?php
function hyperlink($text) {
	// match
protocol://address/path/file.extension?some=variable&ano
ther=asf%
	$text =
preg_replace("/s([a-zA-Z]+://[a-z][a-z0-9_.-]*
			[a-z]{2,6}[a-zA-Z0-9/*-?&%]*)([s|.|,])/i&quo
t;, 
			" <a href="$1"
target="_blank">$1</a>$2",
$text);
	// match
www.something.domain/path/file.extension?some=variable&a
nother=asf%
	$text = preg_replace("/s(www.[a-z][a-z0-9_.-]*
			[a-z]{2,6}[a-zA-Z0-9/*-?&%]*)([s|.|,])/i&quo
t;, 
			" <a href="http://$1"
target="_blank">$1</a>$2",
$text);
	// match nameaddress
	$text =
preg_replace("/s([a-zA-Z][a-zA-Z0-9_.-]*[a-zA-Z]*
			[a-zA-Z][a-zA-Z0-9_.-]*[a-zA-Z]{2,6})([s|.|,])/
i", 
			" <a
href="mailto://$1">$1</a>$2",
$text);
	return $text;
}
?>
----
Server IP: 85.12.26.134
Probable Submitter: 213.160.196.156
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php

Edit        -- https://master
.php.net/note/edit/78359
Del: integrated  -- h
ttps://master.php.net/note/delete/78359/integrated
Del: useless     -- http
s://master.php.net/note/delete/78359/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78359/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78359/spam
Del: non-english -- 
https://master.php.net/note/delete/78359/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78359/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78359
Reject      -- https://mast
er.php.net/note/reject/78359
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )