tags:

views:

74

answers:

8

lets say i have this string here: $string = 'hello my name is "nicholas cage"'.

i want to separate the words into difference strings like this:

$word1 = 'hello';
$word2 = 'my';
$word3 = 'name';
$word4 = 'is';
$word5 = 'nicholas cage';

For fhe first 4 words I can use explode. but how do i do with word5? I want the first and last name to be one string.

A: 

with explode:) google is your friend! thought it would be harder to find the function for this!

but how to do with Nicholas Cage?

fayer
explode will not keep 'nicholas cage' as one word.
just somebody
A: 

you could do $bits = explode(' ', $string);, which would give you: hello, my, name, is, nicholas, cage, but there's no way for it to know that "nicholas cage" is one entity.

I'm not sure how to do what you'd want, you may need to cross reference a dictionary database and join any words that aren't found.

Edit: I see you have now quoted "nicholas cage", in that case you can use a regex, something like: preg_match('/([\s"])(.*?)$1/', $str, $matches);

Rob
cant i use some other function that search for first " and then the last " and extract it to a string somehow?
fayer
A: 

What you're searching for is: explode()

 $text = 'Hello my name is Nicholas Cage';
 $words = explode(" ", $text);

Best wishes, Fabian

halfdan
A: 

that's done by parsing. google "recursive descent".

just somebody
sounds interesting ;-)
streetparade
+3  A: 

You can use this regular expression:

/"[^"]*"|\S+/

You can use it like this:

<?php
$target = 'Hello my name is "Nicholas Cage"';
$pattern = '/"[^"]*"|\S+/';
$matches = array();
preg_match_all($pattern,$target,$matches);
var_dump($matches);
?>
Mark Byers
take a look at my answer
streetparade
+3  A: 

This can be done using regexp:

$string = 'hello my name is "nicholas cage"';
preg_match_all('/(?:"[^"]*"|\S+)/', $string, $matches);
print_r($matches[0]);

It works as following:

  • Find anythinh which matches:
    • "[^"]*" - anything in double quotes
    • \S+ - more then 1 non-space character

But this result is with quotes. Remove them too:

$words = array_map('remove_starting_ending_quotes', $matches[0]);
print_r($words);

function remove_starting_ending_quotes($str) {
    if (preg_match('/^"(.*)"$/', $str, $matches)) {
     return $matches[1];
    }
    else {
     return $str;
    }
}

Now result looks exactly as expected:

Array
(
    [0] => hello
    [1] => my
    [2] => name
    [3] => is
    [4] => nicholas cage
)
Ivan Nevostruev
this works for ANY php version .. my choice
Scott Evernden
+7  A: 

You could also use the string function: str_getcsv if you wanted. Just call the delimiter " " rather than ",";

Example: $array = str_getcsv($string, " ");

Bill
Nice, I will +1 in 9 minutes when I limit is lifted :) Shortest amount of code, great answer. I tested to be sure, and it works perfectly!
Doug Neiner
works great if your PHP > 5.3
Scott Evernden
A: 

this worked for me

$word1 = 'hello'; 
$word2 = 'my'; 
$word3 = 'name'; 
$word4 = 'is'; 
$word5 = 'nicholas cage';

$my = array($word1,$word2,$word3,$word4,$word5);


function word_split($str=array(),$words=1) {
foreach($str as $str)
{
    $arr = preg_split("/[\s]+/", $str,$words+0);
    $arr = array_slice($arr,0,$words);
    }
    return join(' ',$arr);
}

echo word_split($my,1);

returns nicholas cage

streetparade