preg_split on first occurence of whitespace
Moderator: General Moderators
preg_split on first occurence of whitespace
I am desperately trying to split a string into two pieces at the first occurrence of a whitespace character (either a space or a \t), here's an example:
$fruit = preg_split('/\s/U', 'apple orange banana grape');
I'd like to have the pgrep_split give me an array populated like this:
$fruit[0] == 'apple';
$fruit[1] == 'orange banana grape';
Instead I keep getting an array split on every whitespace character. As you can see I've tried to set the regex as "ungreedy" using the "U" character. What am I missing....is there some other way that makes better sense?
Thanks in advance
$fruit = preg_split('/\s/U', 'apple orange banana grape');
I'd like to have the pgrep_split give me an array populated like this:
$fruit[0] == 'apple';
$fruit[1] == 'orange banana grape';
Instead I keep getting an array split on every whitespace character. As you can see I've tried to set the regex as "ungreedy" using the "U" character. What am I missing....is there some other way that makes better sense?
Thanks in advance
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: preg_split on first occurence of whitespace
Code: Select all
preg_match('/([^\s]+)(.*?)/i', 'apple orange banana grape', $fruit);Explanation: Match everything up the the first whitespace, then match everything else.
Re: preg_split on first occurence of whitespace
Had to change the regex slightly, really it was just that the ? was throwing it off. In any case I appreciate the help. Thanks
ps - here's what I changed it to if anyone else cares:
preg_match('/([^\s]+)(.*)/', 'apple orange banana grape', $fruit);
ps - here's what I changed it to if anyone else cares:
preg_match('/([^\s]+)(.*)/', 'apple orange banana grape', $fruit);
Re: preg_split on first occurence of whitespace
Here's another update:
Code: Select all
preg_match('/^(\S++)(.*)/', 'apple orange banana grape', $fruit);
- \S is shorter and means the same as [^\s].
- Added ^ to anchor the regex to the beginning of the string to prevent needless backtracking.
- Made \S match possessively (using ++). This kills possible needsless backtracking.
Code: Select all
preg_split('/\s+/', 'apple orange banana grape', 2);
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: preg_split on first occurence of whitespace
Truly the king of regex.GeertDD wrote:Here's another update:Code: Select all
preg_match('/^(\S++)(.*)/', 'apple orange banana grape', $fruit);Finally, you can also use preg_split(). Just supply a limit (3rd parameter).
- \S is shorter and means the same as [^\s].
- Added ^ to anchor the regex to the beginning of the string to prevent needless backtracking.
- Made \S match possessively (using ++). This kills possible needsless backtracking.
Code: Select all
preg_split('/\s+/', 'apple orange banana grape', 2);
Re: preg_split on first occurence of whitespace AND hyphen
I am trying to achieve the same thing but with matching whitespace+hyphen+whitespace.
I've tried many variations on the following code, but I can't get it to work:
Please could someone explain what I'm doing wrong?
Thank you!
I've tried many variations on the following code, but I can't get it to work:
Code: Select all
$song = 'Explosions in the Sky – Day Four';
$matches = preg_split('/(\s\-\s+)/', $song, 2);
echo ' . $matches[1] . ' by ' . $matches[0];
// Should print 'Day Four by Explosions in the Sky'
// Instead $matches[1] outputs nothing, but $matches[0] outputs $song un-split
Thank you!
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: preg_split on first occurence of whitespace AND hyphen
joeaston wrote:I am trying to achieve the same thing but with matching whitespace+hyphen+whitespace.
I've tried many variations on the following code, but I can't get it to work:
Please could someone explain what I'm doing wrong?Code: Select all
$song = 'Explosions in the Sky – Day Four'; $matches = preg_split('/(\s\-\s+)/', $song, 2); echo ' . $matches[1] . ' by ' . $matches[0]; // Should print 'Day Four by Explosions in the Sky' // Instead $matches[1] outputs nothing, but $matches[0] outputs $song un-split
Thank you!
Look closely, your two hyphens are not the same. The one in $song is slightly larger.
Also, you don't need to group your regex (put it inside ( and )'s) and you don't need to escape the yphen inside the regex.
So, this shold work:
Code: Select all
$song = 'Explosions in the Sky - Day Four';
$matches = preg_split('/\s-\s/', $song, 2);Code: Select all
$matches = preg_split('/\s+(-|–)\s+/', $song, 2);Re: preg_split on first occurence of whitespace
Thanks for trying prometheuzz, but that ain't working! The string still isn't being split.
I don't think it's the hyphen that's the problem. Here's my real code where the hyphen has been copied and pasted:
Any other suggestions?
I don't think it's the hyphen that's the problem. Here's my real code where the hyphen has been copied and pasted:
Code: Select all
$matches = preg_split('/\s–\s/', $item->get_title(), 2);
// $item->get_title() returns something like 'Monta – Long Live the Quiet' (no quotes)- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: preg_split on first occurence of whitespace
Then there's probably more going wrong, but I am sure that the hyphens are different, and can cause problems.joeaston wrote:Thanks for trying prometheuzz, but that ain't working!
...
This works perfectly for me:
Code: Select all
#!/usr/bin/php
<?php
print_r(preg_split('/\s+(-|–)\s+/', 'Explosions in the Sky - Day Four', 2)); // short hyphen
print_r(preg_split('/\s+(-|–)\s+/', 'Explosions in the Sky – Day Four', 2)); // longer hyphen
/* output:
Array
(
[0] => Explosions in the Sky
[1] => Day Four
)
Array
(
[0] => Explosions in the Sky
[1] => Day Four
)
*/
?>Re: preg_split on first occurence of whitespace
You were right!
I went on to Wikipedia and compiled a huge list of different hyphen types. That eventually got it working.
I've no idea which one is which though.
Thanks for your help.
I went on to Wikipedia and compiled a huge list of different hyphen types. That eventually got it working.
I've no idea which one is which though.
Code: Select all
$matches = preg_split('/\s+(-|?|?|–|—|?)\s+/', $item->get_title(), 2);Thanks for your help.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: preg_split on first occurence of whitespace
I won't say "I told you so!"... Oh, dammit, now I did.joeaston wrote:You were right!
I went on to Wikipedia and compiled a huge list of different hyphen types. That eventually got it working.
I've no idea which one is which though.
Code: Select all
$matches = preg_split('/\s+(-|?|?|–|—|?)\s+/', $item->get_title(), 2);![]()
Thanks for your help.
; )
You're welcome, of course.