My string:
fields[name_1]
I want to get fields
and name_1
using regex.
I'm know about preg_match_all()
, but I'm not friends with regular expressions.
My string:
fields[name_1]
I want to get fields
and name_1
using regex.
I'm know about preg_match_all()
, but I'm not friends with regular expressions.
This can be used for direct match:
$string = 'fields[name_1]';
preg_match('/(.+)\[(.+)\]/', $string, $matches);
print_r($matches);
You get:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)
So, $matches[1]
and $matches[2]
are what you needed.
Still I am unclear about your exact need!
Here are the explanation for the Regex:
THere are uncounted examples for this alone here on SO. A simple search would have shown you what you need. Anyway, to get you going:
<?php
$subject = 'fields[name_1]';
preg_match('/^(.+)\[(.+)]$/', $subject, $tokens);
print_r($tokens);
The output of that obviously is:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)