Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

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

5 Answers

0 votes
function getLowercaseWordsString(string $s): string {
    $words = explode(" ", $s);
    $result = "";

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

    return $result;
}

$s = "PHP rust JavaScript c C++ java Typescript python";

echo getLowercaseWordsString($s);



/*
run:

rust c java python 

*/

 



answered Feb 3, 2017 by avibootz
edited Jan 16 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
0 votes
function getLowercaseWords(string $s): array {
    $words = explode(" ", $s);
    $result = [];

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

    return $result;
}

$s = 'PHP rust JavaScript c C++ java Typescript python';

$lowercaseWords = getLowercaseWords($s);

print_r($lowercaseWords);



/*
run:

Array
(
    [0] => rust
    [1] => c
    [2] => java
    [3] => python
)

*/

 



answered Jan 16 by avibootz
0 votes
function getLowercaseWords(string $s): array {
    preg_match_all('/\b[a-z]\w*/', $s, $matches);

    return $matches[0];
}

$s = 'PHP rust JavaScript c C++ java Typescript python';

$lowercaseWords = getLowercaseWords($s);

print_r($lowercaseWords);



/*
run:

Array
(
    [0] => rust
    [1] => c
    [2] => java
    [3] => python
)

*/

 



answered Jan 16 by avibootz

Related questions

...