1

Sorry for dropping an other preg_match regular expression question. I have been dealing with this issue for too long and can't get it to work, so any help is appreciated!!

I need a simple preg_match_all to return me placeholder for the format [@varname].

in a string

"hello, this is [@firstname], i am [@age] years old"

I would expect an array with the values 'firstname' and 'age'.

my attempt for [varname] that worked was:

preg_match_all("/[([^]]+)]/", $t, $result);

anything i tried to get the @character included failed...

preg_match_all("/[\@([^]]+)]/", $t, $result);
4

2 に答える 2

3

コード:

$str = 'hello, this is [@firstname], i am [@age] years old';
preg_match_all('~\[@(.+?)\]~', $str, $matches);
print_r($matches);

出力:

Array
(
    [0] => Array
        (
            [0] => [@firstname]
            [1] => [@age]
        )
    [1] => Array
        (
            [0] => firstname
            [1] => age
        )
)
于 2013-09-10T17:59:13.463 に答える
-1
<?php
$string = "hello, this is [@firstname], i am [@age] years old";
$pattern = '#\[@(.*?)\]#';
preg_match_all($pattern, $string, $matches);
print_r($matches[1]);
?>

Array
(
    [0] => firstname
    [1] => age
)
于 2013-09-10T18:01:08.233 に答える