preg_replace

Hi experts!

I have a function that replaces the accents by letters without signs like below and I want to call once time the preg_replace function to do it. Any idea?

Thanks in advance!
public static function removeAccents	(	$_s	// string to remove accents
											)
	{
		$_s = preg_replace("[á]", "a", $_s);
		$_s = preg_replace("[à]", "a", $_s);
		$_s = preg_replace("[â]", "a", $_s);
		$_s = preg_replace("[ã]", "a", $_s);
		$_s = preg_replace("[ª]", "a", $_s);
		$_s = preg_replace("[Á]", "A", $_s);
		$_s = preg_replace("[À]", "A", $_s);
		$_s = preg_replace("[Â]", "A", $_s);
		$_s = preg_replace("[Ã]", "A", $_s);
		$_s = preg_replace("[é]", "e", $_s);
		$_s = preg_replace("[è]", "e", $_s);
		$_s = preg_replace("[ê]", "e", $_s);
		$_s = preg_replace("[É]", "E", $_s);
		$_s = preg_replace("[È]", "E", $_s);
		$_s = preg_replace("[Ê]", "E", $_s);
		$_s = preg_replace("[í]", "i", $_s);
		$_s = preg_replace("[ì]", "i", $_s);
		$_s = preg_replace("[î]", "i", $_s);
		$_s = preg_replace("[Í]", "I", $_s);
		$_s = preg_replace("[Ì]", "I", $_s);
		$_s = preg_replace("[Î]", "I", $_s);
		$_s = preg_replace("[ó]", "o", $_s);
		$_s = preg_replace("[ò]", "o", $_s);
		$_s = preg_replace("[ô]", "o", $_s);
		$_s = preg_replace("[õ]", "o", $_s);
		$_s = preg_replace("[º]", "o", $_s);
		$_s = preg_replace("[Ó]", "O", $_s);
		$_s = preg_replace("[Ò]", "O", $_s);
		$_s = preg_replace("[Ô]", "O", $_s);
		$_s = preg_replace("[Õ]", "O", $_s);
		$_s = preg_replace("[ú]", "u", $_s);
		$_s = preg_replace("[ù]", "u", $_s);
		$_s = preg_replace("[û]", "u", $_s);
		$_s = preg_replace("[Ú]", "U", $_s);
		$_s = preg_replace("[Ù]", "U", $_s);
		$_s = preg_replace("[Û]", "U", $_s);
		$_s = str_replace("ç", "c", $_s);
		$_s = str_replace("Ç", "C", $_s);
		return $_s;
	}

Open in new window

ZumbaJrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
For all characters which share a common replacement, you can consolidate the search. For example:

$_s = preg_replace("[áàâãª]", "a", $_s);

Open in new window

0
käµfm³d 👽Commented:
For one single preg_replace call, you could do:
$find = array('/[áàâãª]/u', '/[ÁÀÂÃ]/u', '/[éèê]/u', '/[ÉÈÊ]/u', '/[íìî]/u', '/[ÍÌÎ]/u', '/[óòôõº]/u', '/[ÓÒÔÕ]/u', '/[úùû]/u', '/[ÚÙÛ]/u', '/ç/u', '/Ç/u');
$replace = array('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'c', 'C');

$result = preg_replace($find, $replace, $source);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Here is the teaching example showing how I do it, neatly packaged as a one-statement function.  HTH, ~Ray
<?php // RAY_westernize_letters.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO TRANSLATE SOME WESTERN CHARACTERS INTO ENGLISH-PRINTABLE


// TEST CASES
$arr
= array
( 'Françoise'
, 'ßeta or Beta?'
, 'ENCYCLOPÆDIA'
, 'ça va! mon élève mi niña?'
, 'A stealthy ƒart'
, 'Jean "Ðango" Reinhardt of Pont-à-Celles'
)
;

// DISPLAY EACH TEST CASE
foreach ($arr as $str)
{
    echo PHP_EOL
    . '<br/>'
    . $str
    . ' = '
    . '<strong>'
    . mungstring($str)
    . '</strong>'
    ;
}

// EXAMPLE SHOWING HOW TO TURN A PORTUGESE NAME INTO PART OF A URL STRING
$str = 'Armação de Pêra';
$new = mungString($str);
$new = strtolower($new);
$new = str_replace(' ', '-', $new);

// SHOW THE URL STRING
echo PHP_EOL
. '<br/>'
. '<strong>'
. '<a target="blank" href="http://lmgtfy.com?q='
. htmlentities(mungstring($new))
. '">'
. $str
. '</a>'
. '</strong>'
;

// A FUNCTION TO RETURN THE WESTERNIZED STRING
function mungString($str, $return='TEXT')
{
    // OUR REPLACEMENT ARRAY (MAY WANT SOME CHANGES HERE)
    static
    $normal
    = array
    ( 'ƒ' => 'f'  // http://en.wikipedia.org/wiki/%C6%91 florin
    , 'Š' => 'S'  // http://en.wikipedia.org/wiki/%C5%A0 S-caron (voiceless postalveolar fricative)
    , 'š' => 's'  // http://en.wikipedia.org/wiki/%C5%A0 s-caron
    , 'Ð' => 'Dj' // http://en.wikipedia.org/wiki/Eth (voiced dental fricative)
    , 'Ž' => 'Z'  // http://en.wikipedia.org/wiki/%C5%BD Z-caron (voiced postalveolar fricative)
    , 'ž' => 'z'  // http://en.wikipedia.org/wiki/%C5%BD z-caron
    , 'À' => 'A'
    , 'Á' => 'A'
    , 'Â' => 'A'
    , 'Ã' => 'A'
    , 'Ä' => 'A'
    , 'Å' => 'A'
    , 'Æ' => 'E'
    , 'Ç' => 'C'
    , 'È' => 'E'
    , 'É' => 'E'
    , 'Ê' => 'E'
    , 'Ë' => 'E'
    , 'Ì' => 'I'
    , 'Í' => 'I'
    , 'Î' => 'I'
    , 'Ï' => 'I'
    , 'Ñ' => 'N'
    , 'Ò' => 'O'
    , 'Ó' => 'O'
    , 'Ô' => 'O'
    , 'Õ' => 'O'
    , 'Ö' => 'O'
    , 'Ø' => 'O'
    , 'Ù' => 'U'
    , 'Ú' => 'U'
    , 'Û' => 'U'
    , 'Ü' => 'U'
    , 'Ý' => 'Y'
    , 'Þ' => 'B'
    , 'ß' => 'Ss'
    , 'à' => 'a'
    , 'á' => 'a'
    , 'â' => 'a'
    , 'ã' => 'a'
    , 'ä' => 'a'
    , 'å' => 'a'
    , 'æ' => 'e'
    , 'ç' => 'c'
    , 'è' => 'e'
    , 'é' => 'e'
    , 'ê' => 'e'
    , 'ë' => 'e'
    , 'ì' => 'i'
    , 'í' => 'i'
    , 'î' => 'i'
    , 'ï' => 'i'
    , 'ð' => 'o'
    , 'ñ' => 'n'
    , 'ò' => 'o'
    , 'ó' => 'o'
    , 'ô' => 'o'
    , 'õ' => 'o'
    , 'ö' => 'o'
    , 'ø' => 'o'
    , 'ù' => 'u'
    , 'ú' => 'u'
    , 'û' => 'u'
    , 'ý' => 'y'
    , 'ý' => 'y'
    , 'þ' => 'b'
    , 'ÿ' => 'y'
    )
    ;
    // RETURN THE "TRANSLATED" TEXT
    if ($return == 'TEXT') return strtr($str, $normal);

    // MIGHT BE USEFUL TO GET THE LIST OF ORIGINAL LETTERS
    return array_keys($normal);
}

Open in new window

0
ZumbaJrAuthor Commented:
Very good!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.