note 123570 deleted from function.ucwords by cmb

From: Date: Thu, 31 Jan 2019 11:03:53 +0000
Subject: note 123570 deleted from function.ucwords by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210151@lists.php.net to get a copy of this message
Note Submitter: codemilitant at gmail dot com ---- When updating titles of web pages and posts, I found it much faster, and very easy to take advantage of the str_ireplace array search capabilities. This simple function will find only those words you wish to leave in lowercase (or uppercase), and do so with the speed of str_replace instead of any regular expressions. When run, the str_ireplace case insensitivity only applies to the search. When found, it replaces with the word as it's typed in the very same array. Since this project was for web development, I added the ";" and ">" to the ucwords capabilities. Just be sure the words in the delimiters array are the proper case to get the desired results. $title = "Building And CREATING Great Seo WebSites"; function ucproper($string) { $string = ucwords( strtolower(trim($string)), ";> \t\r\n\f\v" ); $delimiters = [ 'and', 'but', 'or', 'of', 'to', 'SEO' ]; $string = str_ireplace( $delimiters, $delimiters, $string ); return $string; //this returns lowercase (or uppercase) delimiter word found in string using those same delimiters as a case insensitive search } echo ucproper($title); //Building and Creating Great SEO Websites

« previous php.notes (#210151) next »