0

特定の正規表現の一致についてサポートが必要です。これはphpです。(ワードプレスプラグインの編集)

文字列が

"[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
$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 に答える
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 に答える