Eliminar espacios en blanco de una cadena con PHP
Cuando trabajamos con formularios, debemos asegurar que los datos solicitados llegan a la base de datos o al fichero con una cierta integridad, a fin de tener un sistema homogéneo y bien ordenado. Una de las funciones de manejo de cadenas que nos ayudarán en este tarea será la función trim(). Con ella, eleminaremos los espacios en blanco existentes al principio y al final de una cadena, quedando aislada la palabra o frase introducida.
Supongamos que tenemos un formulario con una caja de texto, en la que el usuario ha metido la siguiente cadena (metida en la variable $cadena):
Función trim() en PHP
Como puedes observar, delante de la palabra función hay unos cuantos espacios en blanco, inútiles e irrelevantes, al igual que después de PHP. ¿Por qué meter en nuestra base de datos un dato sin pureza? ¿Por qué malgastar espacio? Quizás no es muy frecuente que el usuario actue de esa manera… pero no lo podemos asegurar y, por tanto, debemos controlarlo. El esfuerzo no es grande, pues sólo es una línea y nos merecerá la pena.
$cadena_sin_espacios = trim($cadena);
Dando como resultado:
Función trim() en PHP
Hola,
No era exactamente lo que buscaba, pero me has iluminado 😉
Saludos
Why didnt I think about this? I hear exactly what youre saying and Im so happy that I came across your blog. You truly know what youre talking about, and you made me feel like I ought to learn much more about this. Thanks for this; Im officially a huge fan of your blog 114690
I will surely foreward this post to all of my pals! Its very excellent and a very fine read! 🙂
XVVftg great this on this blog
Some really nice and utilitarian information on this internet site, also I believe the design and style has got fantastic features.
I was examining some of your content on this website and I believe this web site is very informative ! Keep posting .
Precisely what I was looking for, thanks for putting up. «If you can imagine it,You can achieve it.If you can dream it,You can become it.» by William Arthur Ward.
Nice read, I just passed this onto a friend who was doing a little research on that. And he just bought me lunch because I found it for him smile Thus let me rephrase that: Thank you for lunch! «Love is made in heaven and consummated on earth.» by John Lyly.
What’s up, after reading this remarkable piece of writing i am also happy to share my experience here with friends.
We’re a gaggle of volunteers and opening a new scheme in our community. Your website provided us with useful info to paintings on. You’ve performed an impressive task and our entire neighborhood can be grateful to you.
I adore gathering useful info, this post has got me even more info! .