|
Posted by Philip Ronan on 10/25/05 16:14
"Dummy Newgroup name" wrote:
> I'm pulling chunks out of a line of text. In this instance the bit I
> want is delimited by square braces; '[' and ']'.
>
> Is there a single php function that I can do this with?
>
> I know how to do it with multiple functions (something like
> substr($string, strpos($string, '['), strpos($string, ']'))(yes, I
> know that's not completely correct), but php seems so rich in its
> string functions I wondered if there's a single function that will do
> it.
Of course there is: <http://php.net/preg_match>
Try this for example (you might need to debug it first):
<?php
$string = "this [foo] is [bar] a [baz] test";
$re = '/\[([^\]]+)\]/';
preg_match($re, $string, $matches);
print_r($matches);
?>
--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
[Back to original message]
|