?
Solved

preg_replace

Posted on 2011-10-06
4
Medium Priority
?
743 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ZumbaJr
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36924888
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36925044
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36931065
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
 

Author Closing Comment

by:ZumbaJr
ID: 36933269
Very good!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question