I have a text like this
[name:roccos] [phone:123324324] [tags:abc def ghi] [id:value]
How can i find the values of name, phone, etc using php, So i should have variable like this $name = 'roccos'
$phone = '123324324'
, I can either do this for all separately or is there any way to automatically get all variables along with value automatically...
Ex : In this text [abc:def] [phone:123456]
everything before the colon :
will be the variable name and after the colon will be it's value => $abc = 'def'
$phone = '123456'
Just use a regular expression, and preg_match_all
:
$str = '[name:roccos] [phone:123324324] [tags:abc def ghi] [id:value]';
preg_match_all('/\[(.*?):(.*?)\]/', $str, $matches, PREG_SET_ORDER);
$data = array();
foreach($matches as $match) {
$data[$match[1]] = $match[2];
}
Here's a demo. Note that I used an array to hold the data instead of directly assigning them to global variables; that would be dangerous if you were taking this from any kind of user input. If the data is safe, you can expand it using extract
:
extract($data);