Removing Accents and Transforming Spaces into PHP strokes

Asked

Viewed 3,324 times

1

I updated my PHP and my following Script which I used to transform the text without accents, and the spaces in strokes (-) no longer works in PHP due to the ereg_replace is obsolete.

<?php
    $variavel = "Céu Azul";
    $variavel_limpa = strtolower( ereg_replace("[^a-zA-Z0-9-]", "-", strtr(utf8_decode(trim($variavel)), utf8_decode("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ"),"aaaaeeiooouuncAAAAEEIOOOUUNC-")) );
    echo $variavel_limpa; // ceu-azul
?>

What would be the solution for me to perform the transformation of this string (removing accents and placing dash between spaces) to work in PHP 7.

3 answers

3

Like the ereg_replace has been deprecated, it is necessary to replace by another function. Instead of this function, you can use preg_replace, the working is very similar and the rules - practically - are the same.

To remove accented characters, you can use the function iconv, so you will "translate" some letters, for example:

<?php

$text = "O céu azul foi visto por André, João…";

/**
 * Converte a String para ASCII
 * O //TRANSLIT irá tentar traduzir os caracteres, por exemplo è => "`e"
 * Após isso, aplicamos uma expressão regular para deixar somente 
 * \w = Números, Letras e "underline"; e \s = espaço
 */
$text = preg_replace("/[^\w\s]/", "", iconv("UTF-8", "ASCII//TRANSLIT", $text));

/* Com o str_replace podemos substituir os espaços 
 * deixados na linha anterior, pelo hífen
 */
$text = str_replace(" ", "-", $text);

/* Transformamos todo o texto em minúsculo */
$text = strtolower($text);


echo $text;
// Output: o-ceu-azul-foi-visto-por-andre-joao

Demonstration: https://ideone.com/0VC8tb

  • It worked great! Thank you!

  • Because this is the answer that points the best way, which makes a "check" of the characters, whether ANSI (Ios-8859-1 or windows-1252) or utf-8, transcribes in ASCII if possible and what is not possible you eliminate with the metacharacters, making it something functional, different from the utf8_encode that always expects UTF-8 and that will not solve "any input". I’m not saying it’s 100% infallible, it might "eat" a few letters, but it’s a much more efficient internal resource than putting together a range àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ and use utf8_decode. + 1

2


Use the preg_replace, supported in PHP 7 as well.:

$variavel_limpa = strtolower( preg_replace("/[^a-zA-Z0-9-]/", "-", strtr(utf8_decode(trim($variavel)), utf8_decode("áàãâéêíóôõúüñçÁÀÃÂÉÊÍÓÔÕÚÜÑÇ"),"aaaaeeiooouuncAAAAEEIOOOUUNC-")) );

The boundaries must also be included // in regex.

Test here

  • Hello friend @dvd worked great! Before its edition was no "-" between the spaces. now it worked!

  • It is that had forgotten the detail of the delimiters. Worth!

1

strtolower - Converts a string to lowercase

strtr - translates certain characters into a string

utf8_decode - converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1.

$variavel = "Céu Azul";
$variavel_limpa = strtolower(strtr(utf8_decode($variavel), utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ '), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY-'));

example - PHP Sandbox

strtr and str_replace, essentially do the same thing, being that strtr is a little faster for simple operations.

For simple things any attempt to use regular expression fatally will be slower. Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.