0

I need help with a particular regex match. This is php. (Editing a wordpress plugin)

Let's say the string is

"[youtube|sjdhskajxn|This is a string|This is also a string|44|55]"

I want to extract

{0} -> youtube
{1} -> sjdhskajxn
{2} -> This is a string
{3} -> This is also a string
{4} -> 44
{5} -> 55

Also the number of items to be matched will not be constant.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
  • 3
    Just use explode($delimiter, $string); – Damien Pirsy May 01 '13 at 12:31
  • I think you need [explode](http://php.net/manual/en/function.explode.php) by `|` sign. – Rikesh May 01 '13 at 12:31
  • That would leave the brackets, though. – Tim Pietzcker May 01 '13 at 12:31
  • Do you need to be able to escape the `|` or square brackets somehow so that a string can have a `|` in it if need be? – Wolfman Joe May 01 '13 at 12:36
  • like here http://stackoverflow.com/questions/1374643/use-preg-replace-to-replace-character-unless-an-escape-character-precedes – Waygood May 01 '13 at 12:38
  • @WolfmanJoe I can't see that being a requirement. I will be in full control of the inputs.. It's highly unlikely I will have a string with | in it – Dave Lawrence May 01 '13 at 12:39
  • If that is a wordpress plugin, rewrite it to use the [shortcode API](http://codex.wordpress.org/Shortcode_API) - not that it is perfect (your regex is equally limited at best) - it's also the standard way to such stuff in plugins. Do no re-invent the wheel. – hakre May 01 '13 at 12:49
  • 1
    @Waygood That's how I would begin to write a plugin but the already in existence plugin I'm using has regex. I have it working now anyway. Thanks. – Dave Lawrence May 01 '13 at 12:58

4 Answers4

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
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • Trivially works, but it wouldn't work if you need to have `|` or the square brackets as part of the contained text, with an escape character. – Wolfman Joe May 01 '13 at 12:38
1

Use explode() function

$str = "[youtube|sjdhskajxn|This is a string|This is also a string|44|55]";
$str = str_replace(array('[',']'), '', $str);
$pieces = explode("|", $str);
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
  • `trim()` with `[]` is an alternative to `str_replace()`. It works on the ends of the string not the entire string – Waygood May 01 '13 at 12:37
1

If you want to allow Unicode characters:

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

Otherwise (just ASCII), it's simpler:

preg_match_all('/[a-z0-9\s]+/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
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);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93