Table of Contents
php读取文本去除空格
Home php教程 php手册 php读取文本去除空格

php读取文本去除空格

Jun 13, 2016 am 08:53 AM
text space

php读取文本去除空格

  在使用php对mysql进行处理时,有时候要对文件内容进行读取并输出,处理时发现php对读取的文件内容输出字符串后面会有一个空格,从而影响某些操作

 

例如:文件 2.txt 内容如下

 

 

123
234
abc
Copy after login

php脚本

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>";
?>
Copy after login

如图1

查阅资料发现可以通过PHP trim()函数解决此问题

方法如下:

trim() 去除一个字符串两端空格,

rtrim() 是去除一个字符串右部空格,

ltrim() 是去除一个字符串左部空格。

-------------------------------------

更改脚本:

<?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>";
>
Copy after login

 

 

如图2  

 

 

OK  完美解决

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does a space mean in mysql? What does a space mean in mysql? Jul 20, 2023 pm 05:17 PM

MySQL A space is a special character used to separate keywords, identifiers, values, and other statement elements. Spaces can be used in multiple locations in a SQL statement and usually do not affect the execution of the statement. The purpose of spaces: 1. Used to separate keywords and identifiers; 2. Used to separate operators and values; 3. Used to separate functions, columns and table aliases; 4. Used for indentation and alignment.

Try new ringtones and text tones: Experience the latest sound alerts on iPhone in iOS 17 Try new ringtones and text tones: Experience the latest sound alerts on iPhone in iOS 17 Oct 12, 2023 pm 11:41 PM

In iOS 17, Apple has overhauled its entire selection of ringtones and text tones, offering more than 20 new sounds that can be used for calls, text messages, alarms, and more. Here's how to see them. Many new ringtones are longer and sound more modern than older ringtones. They include arpeggio, broken, canopy, cabin, chirp, dawn, departure, dolop, journey, kettle, mercury, galaxy, quad, radial, scavenger, seedling, shelter, sprinkle, steps, story time , tease, tilt, unfold and valley. Reflection remains the default ringtone option. There are also 10+ new text tones available for incoming text messages, voicemails, incoming mail alerts, reminder alerts, and more. To access new ringtones and text tones, first, make sure your iPhone

The difference between full-width spaces and half-width spaces The difference between full-width spaces and half-width spaces Mar 25, 2024 pm 12:45 PM

The difference between full-width spaces and half-width spaces. When we use word processing software or edit text content, we sometimes encounter the concept of spaces. Space is a very basic element in typesetting and formatting text, but many people may not know the difference between full-width spaces and half-width spaces. In daily use, we may feel that full-width spaces and half-width spaces have different effects in different situations, but we may not be aware of the subtle differences. First of all, the difference between full-width spaces and half-width spaces is the width they occupy.

Using large models to create a new paradigm for text summary training Using large models to create a new paradigm for text summary training Jun 10, 2023 am 09:43 AM

1. Text task This article mainly discusses the method of generative text summarization, and how to use contrastive learning and large models to implement the latest generative text summarization training paradigm. It mainly involves two articles, one is BRIO: Bringing Order to Abstractive Summarization (2022), which uses contrastive learning to introduce ranking tasks in the generative model; the other is OnLearning to Summarize with Large Language Models as References (2023), which further introduces large models to generate high-quality training data based on BRIO. 2. Generative text summarization training methods and

How to search for text across all tabs in Chrome and Edge How to search for text across all tabs in Chrome and Edge Feb 19, 2024 am 11:30 AM

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

What to do if win7 system cannot open txt text What to do if win7 system cannot open txt text Jul 06, 2023 pm 04:45 PM

What should I do if win7 system cannot open txt text? When we need to edit text files on our computers, the easiest way is to use text tools. However, some users find that their computers cannot open txt text files. So how to solve this problem? Let’s take a look at the detailed tutorial to solve the problem of unable to open txt text in win7 system. Tutorial to solve the problem that win7 system cannot open txt text. 1. Right-click any txt file on the desktop. If there is no txt file, you can right-click to create a new text document, and then select properties, as shown below: 2. In the opened txt properties window , find the change button under the general options, as shown in the figure below: 3. In the pop-up open mode setting

After chatting online for a month, the pig-killing scammer was actually defeated by AI! 2 million netizens shouted shocked After chatting online for a month, the pig-killing scammer was actually defeated by AI! 2 million netizens shouted shocked Apr 12, 2023 am 09:40 AM

Speaking of "pig killing plate", everyone must hate it with itch. In this type of online dating and marriage scams, scammers will look for victims in advance who are easy to fall for, and they are often innocent, kind-hearted and well-behaved girls with beautiful fantasies about love. In order to fight these scammers for 500 rounds, "Turing's Cat", a well-known up-and-comer in the technology circle at Station B, trained an AI that frequently makes hilarious jokes in chat, and is even better than a real person. As a result, with the operation of AI, the scammer was confused by this fake lady and directly transferred 520 to "her". What’s even funnier is that after discovering that the scammer had no chance to take advantage of him, not only did he break his defense, but he was also given a “famous quote” by the AI: As soon as the video came out, it immediately went viral, and friends surfing at station B were all fooled.

How to copy text from screenshots on Windows 11 How to copy text from screenshots on Windows 11 Sep 20, 2023 pm 05:57 PM

Download the new Snipping Tool with text actions Although the new Snipping Tool is limited to development and canary versions, if you don’t want to wait, you can install the updated Windows 11 Snipping Tool (version number 11.2308.33.0) now. How this works: 1. Go ahead and open this website (visit) on your Windows PC. 2. Next, select "Product ID" and paste "9MZ95KL8MR0L" into the text field. 3. Switch to the "Quick" ring from the right drop-down menu and click Search. 4. Now, look for this version “2022.2308.33.0” in the search results that appear. 5. Right click on the one with MSIXBUNDLE extension and in context menu

See all articles