PDA

View Full Version : preg_split


mr_ego
08-15-2004, 11:02 AM
How do you use a regular expression to turn:

"hello my name" is mr_ego

into:

Array(
0 => "hello my name",
1 => "is",
2 => "mr_ego"
)

using a PREG_SPLIT Regular Expression

sad69
08-16-2004, 08:22 PM
I have a question about those quotes:
"hello my name" is mr_ego

Do you want:
Array(
0 => '"hello my name"',
1 => 'is',
2 => 'mr_ego'
)

or

Array(
0 => "hello my name",
1 => "is",
2 => "mr_ego"
)

I don't know that you can split it in this way. preg_split wants a pattern, and there's no real pattern here. Are there other strings that you want to run this routine on? If you can list a few of them here, perhaps someone can figure out the pattern among them?

Sadiq.

mr_ego
08-19-2004, 07:43 AM
(the second one)





"hello my name" is mr_ego

Array(
0 => "hello my name",
1 => "is",
2 => "mr_ego"
)



"this is a string" that will "come in preg_split"

Array(
0 => "this is a string",
1 => "that",
2 => "will",
3 => "come in preg_split"
)


Here's the pattern basically:

Split the string into quoted sections (either double " or single quotes ') and then split the remaining words (delimered by a space) into keys of the array.

Just don't know how to do it.