Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Last active December 30, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ocramius/b147fc172a96d73df4be to your computer and use it in GitHub Desktop.
Save Ocramius/b147fc172a96d73df4be to your computer and use it in GitHub Desktop.
<?php
$input = 'o@example.com"
* }
*
* @AnnotationTargetPropertyMethod("Bar")
';
$regex = '/([a-z_\\\\][a-z0-9_\\:\\\\]*[a-z]{1})|((?:[+-]?[0-9]+(?:[\\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?)|("(?:[^"\\n]|"")*")|\\s+|\\*+|(.)/i';
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($regex, $input, -1, $flags);
if (count($matches) === 12) {
die("OK");
}
var_dump($matches);
die("NOPE");
/*Expected `$matches`:
```
array(12) {
[0] =>
array(2) {
[0] =>
string(1) "o"
[1] =>
int(0)
}
[1] =>
array(2) {
[0] =>
string(1) "@"
[1] =>
int(1)
}
[2] =>
array(2) {
[0] =>
string(7) "example"
[1] =>
int(2)
}
[3] =>
array(2) {
[0] =>
string(1) "."
[1] =>
int(9)
}
[4] =>
array(2) {
[0] =>
string(3) "com"
[1] =>
int(10)
}
[5] =>
array(2) {
[0] =>
string(1) """
[1] =>
int(13)
}
[6] =>
array(2) {
[0] =>
string(1) "}"
[1] =>
int(22)
}
[7] =>
array(2) {
[0] =>
string(1) "@"
[1] =>
int(38)
}
[8] =>
array(2) {
[0] =>
string(30) "AnnotationTargetPropertyMethod"
[1] =>
int(39)
}
[9] =>
array(2) {
[0] =>
string(1) "("
[1] =>
int(69)
}
[10] =>
array(2) {
[0] =>
string(5) ""Bar""
[1] =>
int(70)
}
[11] =>
array(2) {
[0] =>
string(1) ")"
[1] =>
int(75)
}
}
```*/
<?php
$input = '@AnnotationTargetPropertyMethod("Foo
* Bar
* Baz")
';
$regex = '/([a-z_\\\\][a-z0-9_\\:\\\\]*[a-z]{1})|((?:[+-]?[0-9]+(?:[\\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?)|("(?:[^"]|"")*")|\\s+|\\*+|(.)/i';
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($regex, $input, -1, $flags);
if (count($matches) === 5) {
die("OK");
}
var_dump($matches);
die("NOPE");
/*Expected `$matches`:
```
array(5) {
[0] =>
array(2) {
[0] =>
string(1) "@"
[1] =>
int(0)
}
[1] =>
array(2) {
[0] =>
string(30) "AnnotationTargetPropertyMethod"
[1] =>
int(1)
}
[2] =>
array(2) {
[0] =>
string(1) "("
[1] =>
int(31)
}
[3] =>
array(2) {
[0] =>
string(27) ""Foo
* Bar
* Baz""
[1] =>
int(32)
}
[4] =>
array(2) {
[0] =>
string(1) ")"
[1] =>
int(59)
}
}
```*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment