RE: [PHP] extracting text
| From: | John W. Holmes | Date: | Tue, 24 Dec 2002 19:08:25 +0000 |
| Subject: | RE: [PHP] extracting text | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-129305@lists.php.net to get a copy of this message | ||
> say i have a line of text like: [blah], [rah] PH33r Us! [moo]
>
> how can I extract all the text in the brackets and set it as a var, im
> totally stuck, my idea was to use strstr() but that wouldnt work as
all
> the
> tags were not at the end :(
Assuming you want to replace [foo] with the value in $foo, you can use
something like this:
$str = "this [foo] is a [test] okay?";
$foo = "crap_foo";
$test = "crap_test";
$nstr = preg_replace("/\[([a-z]+)\]/ie",'\$$1',$str);
echo $str . "<br>---<br>" . $nstr;
No error checking, so if $foo doesn't exist, you'll get a warning and
empty string inserted.
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/