正则表达式模式需要PHP - 字符串和单词的混合

时间:2013-05-01 12:29:21

标签: php regex

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

答案 0 :(得分: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

答案 1 :(得分:1)

使用explode()功能

$str = "[youtube|sjdhskajxn|This is a string|This is also a string|44|55]";
$str = str_replace(array('[',']'), '', $str);
$pieces = explode("|", $str);

答案 2 :(得分: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];

答案 3 :(得分: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);