tags:

views:

152

answers:

7

Take this string:

Israel agrees to significant easing of Gaza blockade

I want to return the capitalised words, separated by a comma, like this:

Israel,Gaza

I imagine it must be possible. Any ideas?

+9  A: 

Split the string into it's words with explode(' '), iterate through the words and check if the word is capitalized by checking if it's first letter ($str[0]) is the same as its uppercase variant (strtoupper($str[0])). You can fill an array with the results and then join(',') it

Patrick Daryll Glandien
Nice. You might have to add something else to strip punctuation marks from the capitalized letters ==> "In the country of France, people it French frogs." returns "France,, French" - not sure if this is what's supposed to happen from the original post. Also, you can cut off leading and trailing whitespace after explode in order to deal with multiple spaces between words.
Peter Ajtai
A: 

A simple way would be to use this regular expression to filter out the capitalized words, and then joining the matches array into a string:

preg_match_all('/[A-Z][a-z]+/', 'Israel agrees to significant easing of Gaza blockade', $matches);
echo implode(',' $matches[0]);

(Answer restored since I guess it isn't really wrong per se...)

BoltClock
Detail is that this would not catch, "AAA" or any acronym as a capitalized word.
Peter Ajtai
A: 

here is some code:

$arr = explode($words, ' ');

for ($word as $words){
    if($word[0] == strtoupper($word[0]){
        $newarr[] = $word;

print join(', ', $newarr);
Ahmet Alp Balkan
`foreach` not `for`
1. Poor formatting 2. Syntax errors 3. Inconsistent variable naming
BoltClock
`$arr` is the array that should be used in the loop. Your explode arguments are backwards, you use the wrong variable in the loop which as per my earlier comment should be a `foreach` not `for` loop. But I can see what you were getting at. See Babiker's answer above.
A: 

You can use a regular expression. Something like the following should get your close:

<?php

$str = 'Israel agrees to significant easing of Gaza blockade';

preg_match_all('/([A-Z]{1}\w+)[^\w]*/', $str, $matches);

print_r($matches);

?>

Edit: My regex was way off.

labratmatt
gives me this error: Unknown modifier 'w'
Steven
My regex was way off. Give it a go with my edit.
labratmatt
+3  A: 

Code as suggested by @Patrick Daryll Glandien.

$stringArray = explode(" ", $string);
foreach($stringArray as $word){
  if($word[0]==strtoupper($word[0])){
    $capitalizedWords[] = $word;
  }
}
$capitalizedWords   = join(",",$capitalizedWords);
//$capitalizedWords = implode(",",$capitalizedWords);
Babiker
thanks that did the trick.
Steven
Thanks be to @Patrick Daryll Glandien.
Babiker
Alternately, you can also do `ucwords($word) == $word`.
Lèse majesté
+1  A: 

Use preg_match_all():

preg_match_all('/[A-Z]+[\w]*/', $str, $matches);

If you need to work with non-English or accent characters, then use:

preg_match_all('/\p{L}*\p{Lu}+\p{L}*/', $str, $matches);

Which should also work for words where the first letter isn't capitalized, but a subsequent letter is as is customary in some languages/words.

Lèse majesté
A: 
$str = 'Israel agrees to significant easing of Gaza blockade';
$result = array();
$tok = strtok($str, ' ');
do {
    if($tok == ucfirst($tok))
        $result[] = $tok;
}
while(($tok = strtok(' ')) !== false);
Kevin