putting a pattern into a variable
Moderator: General Moderators
putting a pattern into a variable
Is it possible to use a regex to find a pattern, and then put that value into a variable?
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.- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
preg_replace() can do that for you, if you want to do it inline.
If you simply want found string, you can use the same pattern with preg_match()/preg_match_all() and supply the $matches argument (last one possible).
Code: Select all
$newstring = preg_replace('#((?:http://)?(www\.[a-z]+\.com))#','<a href="http://\\2">\\1</a>',$string);Yes, just provide a variable where you want the matches to be send as the 4th paramater to preg_match
Edit
~~~~~~~~~~~~~~
Left the thread open for too long, didn't know this was already solved
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.Edit
~~~~~~~~~~~~~~
Left the thread open for too long, didn't know this was already solved
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia