Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将带有占位符的字符串解析为数组_Php_Wildcard_String Parsing - Fatal编程技术网

使用PHP将带有占位符的字符串解析为数组

使用PHP将带有占位符的字符串解析为数组,php,wildcard,string-parsing,Php,Wildcard,String Parsing,我有一根这样的绳子 $input = '%name (%postcode) <%email>'; 最后呢? 它应该识别任何字符串中通配符方案后面的任何通配符。所以函数也应该转换 '%address (%name)' 到 通配符方案不是固定的,因此如果您有更好的解决方案,可以更改这些方案。我摆弄了sscanf(),但由于输入字符串的格式不同,我需要更灵活的格式,它不适合我的需要。这就可以了: $input = '%name (%postcode) <%email>';

我有一根这样的绳子

$input = '%name (%postcode) <%email>';
最后呢?

它应该识别任何字符串中通配符方案后面的任何通配符。所以函数也应该转换

'%address (%name)'

通配符方案不是固定的,因此如果您有更好的解决方案,可以更改这些方案。我摆弄了
sscanf()
,但由于输入字符串的格式不同,我需要更灵活的格式,它不适合我的需要。

这就可以了:

$input = '%name (%postcode) <%email>';
preg_match_all('/%(\w+)/', $input, $m);
$wildcards = $m[1];
print_r($wildcards);
array('address', 'name')
$input = '%name (%postcode) <%email>';
preg_match_all('/%(\w+)/', $input, $m);
$wildcards = $m[1];
print_r($wildcards);
Array
(
    [0] => name
    [1] => postcode
    [2] => email
)