我需要特定正则表达式匹配的帮助。这是php。(编辑wordpress插件)
假设字符串是
"[youtube|sjdhskajxn|This is a string|This is also a string|44|55]"
我想提取
{0} -> youtube
{1} -> sjdhskajxn
{2} -> This is a string
{3} -> This is also a string
{4} -> 44
{5} -> 55
要匹配的项数也不会是固定的
$string = '[youtube|sjdhskajxn|This is a string|This is also a string|44|55]';
$string = str_replace(array('[',']'), '', $string); //remove brackets
$result = explode('|', $string); //explode string into an array
$raw = '[youtube|sjdhskajxn|This is a string|This is also a string|44|55]';
// remove brackets only at beginning/end
$st = preg_replace('/(^[)|(]$)/', '', $raw);
$parts = explode('|', $st);
使用explode()
函数
$str = "[youtube|sjdhskajxn|This is a string|This is also a string|44|55]";
$str = str_replace(array('[',']'), '', $str);
$pieces = explode("|", $str);
如果你想允许Unicode字符:
preg_match_all('/[pLpNpZ]+/u', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
否则(只是ASCII),更简单:
preg_match_all('/[a-z0-9s]+/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];