0

我需要特定正则表达式匹配的帮助。这是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

此外,要匹配的项目数也不会是恒定的。

4

4 回答 4

2
$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
于 2013-05-01T12:33:34.497 回答
1

使用explode()功能

$str = "[youtube|sjdhskajxn|This is a string|This is also a string|44|55]";
$str = str_replace(array('[',']'), '', $str);
$pieces = explode("|", $str);
于 2013-05-01T12:32:55.780 回答
1

如果要允许 Unicode 字符:

preg_match_all('/[\pL\pN\pZ]+/u', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

否则(只是ASCII),它更简单:

preg_match_all('/[a-z0-9\s]+/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
于 2013-05-01T12:33:09.667 回答
1
$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);
于 2013-05-01T12:37:49.980 回答