Array question - maybe UTF?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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(" ", "À Á Â Ã Ä Å Ą Ă Ç Ć Č
Ď Đ Ð È É Ê Ë Ę Ě Ğ Ì Í Î Ï İ Ł
Ľ Ĺ Ñ Ń Ň Ò Ó Ô Õ Ö Ø Ő Ŕ Ř Š
Ś Ş Ť Ţ Ù Ú Û Ü Ů Ű Ý Ž Ź Ż à
á â ã ä å ą ă ç ć č ď đ è é ê ë ę
ě ğ ì í î ï ı ł ľ ĺ ñ ń ň ð ò
ó ô õ ö ø ő ŕ ř ś š ş ť ţ ù ú û ü
ů ű ý ÿ ž ź ż ™ (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@xxxxxxxxx> 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@xxxxxxxxx> 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@xxxxxxxxx>:
> > 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'...
> >


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux