PHP Program to Print Even Length Words in a String Last Updated : 08 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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. Table of ContentUsing explode() and foreach() FunctionsUsing array_filter() and strlen() FunctionsUsing preg_split() and array_filter() FunctionsUsing str_word_count() and array_map() FunctionsUsing explode() and foreach() FunctionsThe 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() FunctionsThe 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 portalUsing preg_split() and array_filter() FunctionsThe 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 portalUsing str_word_count() and array_map() FunctionsAnother 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 Comment More infoAdvertise with us Next Article PHP Program to Print Even Length Words in a String V vkash8574 Follow Improve Article Tags : PHP PHP-string Geeks Premier League 2023 Similar Reads PHP program to find the length of the last word in string We are given a string. We are required to write a program in PHP to find the length of the last word in the string using inbuilt functions. We have already discussed approach of solving this problem here. This article discusses the PHP solution to the problem. Examples: Input : "php exercises" Outpu 2 min read How to get a substring between two strings in PHP? To get a substring between two strings there are few popular ways to do so. Below the procedures are explained with the example.Examples: Input:$string="hey, How are you?"If we need to extract the substring between "How" and "you" then the output should be are Output:"Are"Input:Hey, Welcome to Geeks 4 min read How to Extract Words from given String in PHP ? Extracting words from a given string is a common task in PHP, often used in text processing and manipulation. Below are the approaches to extract words from a given string in PHP: Table of Content Using explode() functionUsing regular expressionsUsing explode() functionIn this approach, we are using 1 min read How to limit string length in PHP ? A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Table of ContentUsing for loopUsing mb_strimwidth() function Using substr() method Using Regular Expressions with preg_m 6 min read PHP str_pad to print string patterns str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-If the value of pad_length is negative, less than, or equal to the length of the i 1 min read Program to generate random string in PHP Given a size N and the task is to generate a random string of size N. Examples: Input: 5Output: eR3DsInput: 10Output: MPRCyBgdcnUsing a Domain String and Random IndexCreate a domain string that contains small letters, capital letters, and the digits (0 to 9). Then generate a random number pick the c 2 min read PHP to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo 2 min read How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to 5 min read How to get string between two characters in PHP ? A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of ContentUsing substr() Method: Using fo 4 min read How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t 2 min read Like