PHP Program to Print Even Length Words in a String
Last Updated :
23 Jul, 2025
This article will show you how to print even-length words in a string using PHP. Printing even-length words from a given string is a common task that can be approached in several ways.
Using explode() and foreach() Functions
The explode() function can be used to split the string into an array of words, and then a foreach loop can be employed to filter and print the words with an even length.
PHP
<?php
function EvenLengthWords($str) {
// Split the string into an
// array of words
$words = explode(' ', $str);
// Iterate through each word and
// print if it has an even length
foreach ($words as $word) {
if (strlen($word) % 2 === 0) {
echo $word . " ";
}
}
}
// Driver code
$str = "Welcome to Geeks for Geeks, A computer science portal";
EvenLengthWords($str);
?>
Outputto Geeks, computer portal
Using array_filter() and strlen() Functions
The array_filter() function can be utilized along with a custom callback function to filter out the words with an even length.
PHP
<?php
function isEvenLength($word) {
return strlen($word) % 2 === 0;
}
function EvenLengthWords($str) {
// Split the string into an
// array of words
$words = explode(' ', $str);
// Use array_filter to keep only
// even-length words
$evenLengthWords = array_filter($words, 'isEvenLength');
// Print the even-length words
echo implode(' ', $evenLengthWords);
}
// Driver code
$str = "Welcome to Geeks for Geeks, A computer science portal";
EvenLengthWords($str);
?>
Outputto Geeks, computer portal
Using preg_split() and array_filter() Functions
The preg_split() function can be employed to split the string using a regular expression, and then array_filter() can be used to filter out words with even lengths.
PHP
<?php
function EvenLengthWords($str) {
// Split the string into an array of
// words using a regular expression
$words = preg_split('/\s+/', $str);
// Use array_filter to keep only
// even-length words
$evenLengthWords = array_filter($words,
function ($word) {
return strlen($word) % 2 === 0;
});
// Print the even-length words
echo implode(' ', $evenLengthWords);
}
// Driver code
$str = "Welcome to Geeks for Geeks, A computer science portal";
EvenLengthWords($str);
?>
Outputto Geeks, computer portal
Using str_word_count() and array_map() Functions
Another approach to print even-length words from a given string is by using the str_word_count() function to split the string into an array of words, and then using the array_map() function to filter and print the even-length words.
Example: This method provides another clean and efficient way to print even-length words from a string, showcasing the versatility of PHP's array and string handling functions.
PHP
<?php
function printEvenLengthWords($inputString) {
// Split the string into an array of words
$words = str_word_count($inputString, 1);
// Filter and print even-length words
array_map(function($word) {
if (strlen($word) % 2 == 0) {
echo $word . "\n";
}
}, $words);
}
// Sample usage
$inputString = "This is a simple test string";
printEvenLengthWords($inputString);
?>
OutputThis
is
simple
test
string
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance