Splitting words by spaces except when they are in "quot

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've updated my previous post, not wanting to flood the page.

I made a couple of improvements in my code and added some profiling stuff. As the numbers show, Ambush's solution is faster every time, albeit wordier, but less confusing to most people here.

:)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It just got even wordier. Added comments. This sort of thing is meant to be edited.

Mine is faster. Heh. That's really cool. :D

::Scratch::

Now you've committed blasphemy there, Ambush Commander, for thinking you could be better than master Feyd. Now... feel his wrath! :lol: Really, I guess, it's a matter of the old belief that regular expressions are expensive.

I found this input very interesting:

Code: Select all

bang th\\is \\"Fo \"o bar" \\\"B"ang Bling" foo "Boo" foo
Both algorithms performed almost the same for it... I wonder why...
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Dudes, you rock :D
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I've gone for the Ambush Commander's solution, mainly because Feyd's uses ctype_space, a function that my version of PHP doesn't support.

Thanks, guys!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can create your own ctype_space():

Code: Select all

if(!function_exists('ctype_space'))
{
function ctype_space($input)
{
  return (strlen(trim($input)) == 0);
}
}
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

That's good, because Ambush Commanders kept timing out on the array merge line :(
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmmm... how big are your strings?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

3 words, in this case :(

Any way it can be done without array_merge ?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Nope. Although I bet it's a bug in the code. Can you post the string?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

"crap car" yuck
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I don't think it is a bug in the code, my PHP has been behaving very oddly lately...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

No, it's a bug. I see it. Unitialized string offset. Working on a fix.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

How come it worked for Feyd?

/confused
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Updated first post with new code.

The problem with the code was it couldn't handle quotes that were at position 0. It would try to find a backslash that was before it (at position -1), which, of course, is unacceptable. With E_ALL on, you see the -1 offset and it's been fixed by adding in an extra conditional and then having the other sections make sure their section isn't empty.

It worked for Feyd because he didn't test any strings that started with a quote. ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't test much of anything in my solution, no need! :P
Post Reply