sábado, 7 de febrero de 2015

remove space and whitespace from a variable

For just spaces, use str_replace:
$string = str_replace(' ', '', $string);




For all whitespace, use preg_replace:

$string = preg_replace('/\s+/', '', $string); 

http://stackoverflow.com/questions/2109325/how-to-strip-all-spaces-out-of-a-string-in-php 




<?php
function string_to_filename($word) {
    $tmp preg_replace('/^\W+|\W+$/'''$word); // remove all non-alphanumeric chars at begin & end of string
    $tmp preg_replace('/\s+/''_'$tmp); // compress internal whitespace and replace with _
    return strtolower(preg_replace('/\W-/'''$tmp)); // remove all non-alphanumeric chars except _ and -}?>

No hay comentarios:

Publicar un comentario