Re: POSIX and PCRE help
| From: | Aaron | Date: | Sat, 16 Nov 2002 09:17:52 +0000 |
| Subject: | Re: POSIX and PCRE help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124713@lists.php.net to get a copy of this message | ||
Lewis,
First I would look at breaking out the tags:
// User: JohnDoe; age=32; nickname="Billy 'the' Kid"; haircolor=orange;
$string = 'User: JohnDoe; age=32; nickname="Billy \'the\' Kid";
haircolor=orange;';
$stringArray = preg_split('/;/', $string, -1, PREG_SPLIT_NO_EMPTY);
Then split by the = or :
// split by = or :
foreach ($stringArray as $item) {
list($tag, $element) = preg_split('/\:|\=/', $item, 1,
PREG_SPLIT_NO_EMPTY);
echo "$tag => $element<br />";
}
-aaron
"Orangehairedboy" <me@orangehairedboy.com> wrote in message
news:20021116071951.35473.qmail@pb1.pair.com...
> I am trying to learn more about regular expressions as I haven't used them
> much in the past. I am working with email, and I'm looking for a way to
> split the following expression up:
>
> Content-Type: text/plain; boundary="whatever";
>
> Using "/^(\S+)\:\s*(.+)$/iU" I can split it into:
>
> [Content-Type] and [text/plain; boundary="whatever";]
>
> Problem is, it might have different tags. Here's a sketch of the whole
> thing:
>
> [Header Name a-zA-Z0-9] [\s*] [:] [\s*] [ Header value a-zA-Z0-9/_ ] [\s*]
> [;] [ unlimited repeating pattern of ( [Property Name a-zA-Z0-9] [\s*] [=]
> [\s*] ( string optionally surrounded by quotes - but necessary if value
has
> spaces - but can't include quotes ) [\s*] [;] ) ]
>
> So, if I had:
>
> User: JohnDoe; age=32; nickname="Billy 'the' Kid"; haircolor=orange;
>
> I would need:
>
> User - "JohnDoe" - age - "32" - nickname - "Billy 'the'
> Kid" - haircolor -
> "orange"
>
> in the outputted array. I have no idea how to do repeating patterns like
> this...maybe I'm making this too complex?
>
> Thanks for your help!
>
> Lewis
>
>