-4
I have this function below:
<?php
$titulo = "Notícia Com Ácêntös";
$titulo_novo = preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), $titulo);
echo $titulo_novo; // Retorna: Notcia-Com-cnts
?>
Note that she’s removing the accents, I want you to just remove the special characters, but keep the accents and ç
(cedilla).
I need this function to rename the images I upload, for example:
Convert that:
"Símbolo cachaça & foguete.com.jpg"
For that reason:
"Símbolo-cachaça-foguete-com.jpg"
For the expected result it seems to me that neither of regex you need, just use https://www.php.net/manual/en/function.strtr.php, remembering to separate the extension from the rest of the string, which can be done with
pathinfo
or until substr + strrpos– Guilherme Nascimento
That way I would have to identify everything I want to replace, I want something automatic that I tell you what I don’t want you to change, and everything else goes away. I think preg_replace is ideal but I’m not doing it right.
– Alex Fonseca
It doesn’t seem ideal, just this looking like this because it solved part of what you already wanted, anyway what I said already applies, even in preg_replace, just you DELIMIT what you need to be removed, instead of "ignore", just replace &, _, . and spaces and finally when it gets hyphens in sequence do another replace just to remove the sequences that will appear.
– Guilherme Nascimento
Look, I used the "&"," example." just as an example, because there are a multitude of characters that users can name the images before sending, for me to make a list of all the code would be very large, understood?
– Alex Fonseca
utf8 and latin1 accents (iso-8859-1, windows-1252, etc.) are not the same, I mean
á
utf-8 is not the same thing asá
of latin1, they look alike, but they’re not the same. Soon the very idea that applying something the way you want it will be something flawed, so there is no way to solve it the way you want it.– Guilherme Nascimento