Home >Backend Development >PHP Tutorial >PHP reads text and removes spaces_PHP tutorial

PHP reads text and removes spaces_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:07:211134browse

php reads text and removes spaces

When using PHP to process mysql, sometimes the file content needs to be read and output. During processing, it is found that there is a space after the PHP output string of the read file content, which affects certain operations.
For example: the content of file 2.txt is as follows
123
234
abc

php script
file.php
<?php
$con = mysql_connect("localhost","root","111111");
if (!$con)
{
die(&#39;Could not connect:&#39; .mysql_error());
}
mysql_select_db("zzadmin",$con);
$file=file(&#39;2.txt&#39;);
$arrlength=count($file);
for ($x=0;$x<$arrlength;$x++) {
$b=$file[$x];
$sql = "INSERT INTO csdn_db (username,passwd,email) SELECT username,passwd,email FROM csdn where passwd like &#39;$b%&#39;";
var_dump($sql);
echo "<p>";
?>

Figure 1
PHP reads text and removes spaces_PHP tutorial
After consulting the information, I found that this problem can be solved through the PHP trim() function
The method is as follows:
trim() removes spaces at both ends of a string,
rtrim() is to remove spaces on the right side of a string,
ltrim() removes spaces from the left of a string.
----------------------------------------
Change script:
<?php
$con = mysql_connect("localhost","root","111111");
if (!$con)
{
die(&#39;Could not connect:&#39; .mysql_error());
}
mysql_select_db("zzadmin",$con);
$file=file(&#39;2.txt&#39;);
$arrlength=count($file);
for ($x=0;$x<$arrlength;$x++) {
$c=$file[$x];
$b=rtrim($c);   
//添加函数
$sql = "INSERT INTO csdn_db (username,passwd,email) SELECT username,passwd,email FROM csdn where passwd like &#39;$b%&#39;";
var_dump($sql);
echo "<p>";
>

As shown in Figure 2
PHP reads text and removes spaces_PHP tutorial
OK perfect solution

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1061305.htmlTechArticlephp reads text to remove spaces. When using php to process mysql, sometimes it is necessary to read the file content And output, during processing, it was found that php will output the string after the read file content...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn