Array question - maybe UTF?

From: Date: Tue, 27 Feb 2007 05:06:37 +0000
Subject: Array question - maybe UTF?
Groups: php.general 
Request: Send a blank email to php-general+get-249477@lists.php.net to get a copy of this message
Perhaps it's more a UTF8 issue that is going wrong for me. At first strtr() was not giving me the results I wanted, so I started tinkering. This is what I came up with... Still no satisfactory results. I think character support is rather lacking in php, or maybe I just don't have a clue? Gerry function xlate_remove_accents( $string, $german=false, $output_array=false ) { // Single letters $single_fr = explode(" ", "À Á Â Ã Ä Å &#260; &#258; Ç &#262; &#268; &#270; &#272; Ð È É Ê Ë &#280; &#282; &#286; Ì Í Î Ï &#304; &#321; &#317; &#313; Ñ &#323; &#327; Ò Ó Ô Õ Ö Ø &#336; &#340; &#344; Š &#346; &#350; &#356; &#354; Ù Ú Û Ü &#366; &#368; Ý Ž &#377; &#379; à á â ã ä å &#261; &#259; ç &#263; &#269; &#271; &#273; è é ê ë &#281; &#283; &#287; ì í î ï &#305; &#322; &#318; &#314; ñ &#324; &#328; ð ò ó ô õ ö ø &#337; &#341; &#345; &#347; š &#351; &#357; &#355; ù ú û ü &#367; &#369; ý ÿ ž &#378; &#380; ™ (r)"); $single_to = explode(" ", "A A A A A A A A C C C D D D E E E E E E G I I I I I L L L N N N O O O O O O O R R S S S T T U U U U U U Y Z Z Z a a a a a a a a c c c d d e e e e e e g i i i i i l l l n n n o o o o o o o o r r s s s t t u u u u u u y y z z z (tm) (r)"); $single = array(); for ($i=0; $i<count($single_fr); $i++) { $single[$single_fr[$i]] = $single_to[$i]; } // Ligatures $ligatures = array("Æ"=>"Ae", "æ"=>"ae", "Œ"=>"Oe", "œ"=>"oe", "ß"=>"ss"); // German umlauts $umlauts = array("Ä"=>"Ae", "ä"=>"ae", "Ö"=>"Oe", "ö"=>"oe", "Ü"=>"Ue", "ü"=>"ue"); // Replace $replacements = array_merge($single, $ligatures); if ($german) $replacements = array_merge($replacements, $umlauts); if ( $output_array ) return( $replacements ); // $string = strtr($string, $replacements); $n = ''; $s = str_split($string); $l = strlen($string); for ( $x = 0 ; $x < $l ; $x++ ) { $c = $s[$x]; $newc = $c ; foreach ( $replacements as $key => $val ) { if ( $key == $c ) $newc = $val ; } $n .= $newc ; } return $n ; } On 2/26/07, Gerry D <gdanen.spam@gmail.com> wrote:
I was hoping for a function or something so I would not have to loop through the array. Then again, I imagine search_array() does that anyway... Thanks for the quick reply! Gerry On 2/26/07, Hap-Hang Yu <qoo2004@gmail.com> wrote: Try: $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); $search = 'apple'; foreach($fruit as $key => $val) { if ($val == $search) {
     echo "$search found: array key is $key, value is $val";
} } 2007/2/27, Gerry D <gdanen.spam@gmail.com>:
I have a question on how to retrieve the value that corresponds to a key in an array. $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
                $key = array_search($c, $fruit);
                if ( $key === FALSE )
                        $n = $c;
                else
                {
                        $n = $fruit[$key];  // how to get the value???
                }
the array_search works ok, but how do I get the value? all I get back is 'a' or 'b', not 'apple' or 'banana'...


« previous php.general (#249477) next »