开发者

Remove the blank spaces

I have an external 开发者_JAVA技巧text file with a list of numbers like this:

35454
54545
45444

I want to output it like this:

'35454' 
'54545' 
'45444' 

I would post my code, but Stack Overflow keeps giving me errors and cutting off my code when I try to post it.


It appears you mean that there's trailing spaces after the numbers?

You're looking for trim()

http://php.net/manual/en/function.trim.php


I'm not sure exactly what you mean but this will do assuming that text.txt contains the values

$vals = file('text.txt');
foreach($vals as $v){
    echo '\''.trim($v).'\'<br />';

}

To remove blank spaces at the beginning and the end you can use trim or to remove all spaces something like $num = str_replace(' ','',$num); could work


If those strings always are numbers, you you can cast them to int like

var_dump((int)'  42  ');

which will output

int(42) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜