How to extract only words with first-letter lowercase from a string in PHP

3 Answers

0 votes
$s = 'PHP html JavaScript css node.js Typescript';
  
$words = explode(" ", $s);
$result = "";

foreach ($words as $w) 
    if (ctype_lower($w[0])) {
        $result .= $w . " ";
    }
  
echo $result;

   
   
   
/*
run:
    
html css node.js
     
*/

 



answered Feb 3, 2017 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
function extract_only_words_with_first_letter_lowercase($s) {
    $words = array();
    $start = 0;
  
    while (($end = strpos($s, " ", $start)) !== false) {
        $word = substr($s, $start,$end - $start);
        if (ctype_lower($word[0])) {
            array_push($words, $word);
        }
        $start = $end + 1;
    }
      
    if (ctype_lower(substr($s, $start)[0])) {
        array_push($words, substr($s, $start));
    }
          
    return $words;
}
  
$s = "PHP is a General-purpose scripting language for web development";
$words = extract_only_words_with_first_letter_lowercase($s);
  
foreach ($words as $w) {
    echo $w,"\n";
}
  
  
  
/*
run:
  
is
a
scripting
language
for
web
development
  
*/

 

 



answered Apr 13, 2024 by avibootz
0 votes
function extract_only_words_with_first_letter_lowercase($s) {
    $words = explode(" ", $s);
    $lowercase = array();
  
    foreach ($words as $word) {
        if (ctype_lower($word[0])) {
            array_push($lowercase, $word);
        }
    }
      
    return $lowercase;
}
  
$s = "PHP is a General-purpose scripting language for web development";
$words = extract_only_words_with_first_letter_lowercase($s);
  
foreach ($words as $w) {
    echo $w,"\n";
}
  
  
  
/*
run:
  
is
a
scripting
language
for
web
development
  
*/

 



answered Apr 13, 2024 by avibootz
...