Forum Moderators: coopster

Message Too Old, No Replies

Retrieving string with regx (preg)

regx

         

newhope

3:52 am on Apr 14, 2008 (gmt 0)

10+ Year Member



Hi,
I have a string :
'<@abc@> asd <@ff@> <@aaa@>'
How can i retrieve 'abc', 'ff' and 'aaa' via preg function in php ?
Also retrieving the string start with '<@' and end with '@>'
I'm stucking in this probl for too long.
plz help me

tutorial

6:09 am on Apr 14, 2008 (gmt 0)

10+ Year Member



You should use function preg_match_all.

This is the code for the first problem:


<?php
$string="<@abc@> asd <@ff@> <@aaa@>";

$pattern='/(abc)/';
preg_match_all($pattern,$string,$result);

echo $result[0][0];
?>

This is the code for the second problem:


<?php
$string="<@abc@> asd <@ff@> <@aaa@>";

$pattern='/<(.*?)>/';
preg_match_all($pattern,$string,$result);

echo $result[0][0];
?>

You have to adjust pattern for the first code, this way it just retrieves 'abc'.