Jump to content

Perl expression to extract URL from text


meman1188

Recommended Posts

<pre>
<?php

$data = <<<DATA
http://www.yahoo.com
yahoo.com
www.yahoo.com/mypage/
DATA;

echo preg_replace_callback('%
	### Protocol or start.
	(?:
		https?://
		|
		www\.
		|
		### Add to as needed.
		\w+\.(?:com|net|org)
	)
	### Body.
	(?>
		### Gobble all non-space
		\S+
		### Avoid ending punctuation.
		(?<!\p{P})
	)?
%x',
create_function(
	'$url',
	'$url = $url[0]; if (substr($url, 0, 7) != "http://") { $url = "http://" . $url; } return "<a href=\"$url\">$url</a>";'
),
$data
);	

?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.