putting a pattern into a variable
Posted: Mon Feb 13, 2006 2:48 pm
Is it possible to use a regex to find a pattern, and then put that value into a variable?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?
$variable = 'this is a message that has a link in it. http://www.ourlacrosse.com';
$url = 'www\\.[a-z]+\\.com'; \\ I want to find any urls and put them into a variable.
print ("<a href='$url'>$url</a>"); \\ Then I want to be able to make the url a link.Code: Select all
$newstring = preg_replace('#((?:http://)?(www\.[a-z]+\.com))#','<a href="http://\\2">\\1</a>',$string);Code: Select all
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.