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
php script
file.php
<?php
$con = mysql_connect("localhost","root","111111");
if (!$con)
{
die('Could not connect:' .mysql_error());
}
mysql_select_db("zzadmin",$con);
$file=file('2.txt');
$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 '$b%'";
var_dump($sql);
echo "<p>";
?>
Figure 1
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('Could not connect:' .mysql_error());
}
mysql_select_db("zzadmin",$con);
$file=file('2.txt');
$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 '$b%'";
var_dump($sql);
echo "<p>";
>
As shown in Figure 2
OK perfect solution
http://www.bkjia.com/PHPjc/1061305.htmlwww.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