Convert a string to Lowercase with exeptions

If you want to convert a string to lower case while some words in upper or without change, then here is function for this

function str_to_lower($str,$leave=array()){
$string= explode(' ', $str);
foreach($string as $k=>$s){
if(!in_array($s, $leave)){
$string[$k]= strtolower($s);
return implode(' ', $string);
You can change it to convert to upper and other text formats.

Comments: 5

  1. Rory C. Schultz July 6, 2013 at 2:03 pm Reply

    Convenience function that returns a RefAppender!(A) object initialized with array. Don’t use null for the array pointer, use the other version of appender instead.

  2. Angelia Potts July 14, 2013 at 9:19 am Reply

    I was looking for a function that would split a string into an array like str_split() and found Razor’s function above. Just though that I would simplify the code a little. <?php function str_split_php4($text, $split = 1){ //place each character of the string into and array $array = array(); for($i=0; $i < strlen($text); $i ){ $key = NULL; for ($j = 0; $j < $split; $j ){ $key .= $text[$i]; } array_push($array, $key); } return $array; } ?> Both mine and worksRazor’s work well, I just prefer to use less code. I could have written one myself, but I was just being lazy.

  3. Bruce K. Scott July 26, 2013 at 8:54 am Reply

    This function is useful when parsing a CSV file with a heading column, but the columns might vary in order or presence: <?php $f = fopen(“file.csv”, “r”); /* Take the first line (the header) into an array, then flip it so that the keys are the column name, and values are the column index. */ $cols = array_flip(fgetcsv($f)); while ($line = fgetcsv($f)) { // Now we can reference CSV columns like so: $status = $line[$cols['OrderStatus']]; } ?> I find this better than referencing the numerical array index.

  4. Jame Hodges August 24, 2013 at 12:47 pm Reply

    If you’re working with very large 2 dimensional arrays (eg 20,000 elements) it’s much faster to do this… <?php $needle = ‘test for this’; $flipped_haystack = array_flip($haystack); if ( isset($flipped_haystack[$needle]) ) { print “Yes it’s there!”; } ?> I had a script that went from 30 seconds down to 2 seconds (when hunting through a 50,000 element array 50,000 times). Remember to only flip it once at the beginning of your code though!

  5. Maggie K. Randolph September 28, 2013 at 1:30 am Reply

    i tried to find a way to create BIG multidimensional-arrays. but the notes below only show the usage of it, or the creation of small arrays like $matrix=array(‘birne’, ‘apfel’, ‘beere’); for an online game, i use a big array (50×80) elements. it’s no fun, to write the declaration of it in the ordinary way. here’s my solution, to create an 2d-array, filled for example with raising numbers. <?php $matrix=array(); $sx=30; $sy=40; $i=1; for ($y=0; $y<$sy; $y ) { array_push($matrix,array()); for ($x=0; $x<$sx; $x ) { array_push($matrix[$y],array()); $matrix[$x][$y]=$i; $i ; } } ?> if there is a better way, plz send an email. i always want to learn more php!