- Php all words first character capitalize in php
- PHP String ucwords() Function
- Related functions
- Syntax
- Parameters
- Return Values
- Changelog
- Examples
- PHP Capitalize each word and after bracket with special characters
- Uppercase the first character of each word in a string except ‘and’, ‘to’, etc
- Upper Case all First Characters in a string Except certain words
- Capitalize all first letters php
Php all words first character capitalize in php
By default these separator characters are: Space \t — tab \n — newline \r — carriage return \f — form feed \v — vertical tab Return Values The ucwords() function returns the modified string, where the first character of each word in a string is converted to uppercase. There are some other functions in PHP which are similar to the ucwords() function: Related functions strtoupper() — It converts a whole string into uppercase. strtolower() — It converts a whole string into lowercase. lcfirst() — It converts only the first character of a string into lowercase.
PHP String ucwords() Function
The ucwords() is an in-built function of PHP, which is used to convert the first character of each word to uppercase in a string. The ucwords() is supported by the PHP 4 and above versions. It takes a string as an input and converts the first character of each word of the string to uppercase. The other characters of the string remain the same.
Note: The ucwords() is a binary-safe function.
There are some other functions in PHP which are similar to the ucwords() function:
Related functions
- strtoupper() — It converts a whole string into uppercase.
- strtolower() — It converts a whole string into lowercase.
- lcfirst() — It converts only the first character of a string into lowercase.
- ucfirst() — It converts only the first character of a string into uppercase.
Syntax
The syntax of the ucwords() function is given below that accepts two parameters.
The ucwords() returns the converted string whose first character of each word is converted to uppercase.
Parameters
$string (required) — It is a mandatory parameter of this function, which specifies the input string that needs to be converted.
$separator (optional) — It is an optional parameter of this function, which contains the words separator characters. It specifies a character that uses a separator for the words in the input string. By default these separator characters are:
- Space
- \t — tab
- \n — newline
- \r — carriage return
- \f — form feed
- \v — vertical tab
Return Values
The ucwords() function returns the modified string, where the first character of each word in a string is converted to uppercase.
Changelog
Examples
There are some examples given, through which we can learn the working of the ucwords() function. Let’s see the below examples-
Hello, My Name Is Lovyansh.
"; $result_str = ucwords($input_str); echo "After: ". $result_str; ?>
Before: Good morning! everyone. After: Good Morning! Everyone.
"; $result_str2 = ucwords($input_str, "|"); echo $result_str2; ?>
In the above example, we have used «|» as a separator, which needs to be passed in ucwords() while modifying the string.
Good|morning!|everyone. Good|Morning!|Everyone.
Note: Doller $ symbol cannot be used as a separator, because $ is used before every variable in PHP. So, the program will generate an error «variable not found.»
PHP ucfirst() Function, lcfirst() — converts the first character of a string to lowercase ; ucwords() — converts the first character of each word in a string to uppercase ; strtoupper()
PHP Capitalize each word and after bracket with special characters
you can use ucwords() function to achieve this.
$text= "BIG LETTERS WITH ÆØÅØÆØÅØÆ (MORE ÆØÆÅØ HERE)"; echo str_replace('( ', '(', ucwords(str_replace('(', '( ', strtolower($text))));
Php capitalize each word Code Example, php ucfirst all words ; 1. $foo = ‘hello world!’; ; 2. $foo = ucwords($foo); // Hello World! ; 3. ; 4. $bar = ‘HELLO WORLD!’; ; 5. $bar = ucwords($bar); // HELLO
Uppercase the first character of each word in a string except ‘and’, ‘to’, etc
None of these are really UTF8 friendly, so here’s one that works flawlessly (so far)
function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI")) < /* * Exceptions in lower case are words you don't want converted * Exceptions all in upper case are any words you don't want converted to title case * but should be converted to upper case, e.g.: * king henry viii or king henry Viii should be King Henry VIII */ $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8"); foreach ($delimiters as $dlnr =>$delimiter) < $words = explode($delimiter, $string); $newwords = array(); foreach ($words as $wordnr =>$word) < if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) < // check exceptions list for any words that should be in upper case $word = mb_strtoupper($word, "UTF-8"); >elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) < // check exceptions list for any words that should be in upper case $word = mb_strtolower($word, "UTF-8"); >elseif (!in_array($word, $exceptions)) < // convert to uppercase (non-utf8 only) $word = ucfirst($word); >array_push($newwords, $word); > $string = join($delimiter, $newwords); >//foreach return $string; >
$s = 'SÃO JOÃO DOS SANTOS'; $v = titleCase($s); // 'São João dos Santos'
since we all love regexps, an alternative, that also works with interpunction (unlike the explode(» «. ) solution)
$newString = preg_replace_callback("/[a-zA-Z]+/",'ucfirst_some',$string); function ucfirst_some($match)
edit added strtolower() , or «Not» would remain «Not».
You will have to use ucfirst and loop through every word, checking e.g. an array of exceptions for each one.
Something like the following:
$exclude = array('and', 'not'); $words = explode(' ', $string); foreach($words as $key => $word) < if(in_array($word, $exclude)) < continue; >$words[$key] = ucfirst($word); > $newString = implode(' ', $words);
PHP lcfirst() Function, Definition and Usage · ucfirst() — converts the first character of a string to uppercase · ucwords() — converts the first character of each word in a string to
Upper Case all First Characters in a string Except certain words
1.You try doing it by first splitting the string by blank space like:
$beforeb="new york branch 2 br property jbr" $afters=explode(" ",$beforeb);
2.Now using for each loop you can match every string and convert it into upper case if it doesnot with words like br or jbr,and store it to an array ex,$array1[]
foreach($afters as $caseup) < $i=0; if($caseup=="br" || $caseup=="jbr")< //selected words you dont want to make uppercase $array1[i]=$caseup; >else < $array1[i]=ucwords($caseup); >>
3.Then again convert the $array1[] into a string using implode() method
Convert the first character to uppercase in every newlines in php, echo ucfirst($str);. Is there a way to solve this using ucfirst() or preg_replace() function ? Thanks! php.
Capitalize all first letters php
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?