How to delete blank strings in php

藏色散人
Release: 2023-03-11 17:02:01
Original
2292 people have browsed it

How to delete blank strings in php: 1. Use trim to remove ordinary spaces on both sides of the string; 2. Use str_replace to delete; 3. Delete through strtr; 4. Use "function trimall($str){ ...}" method to delete.

How to delete blank strings in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to delete blank strings in php? How to remove spaces from string in php?

1. Use the php function trim():

trim($str)
Copy after login

to remove ordinary spaces on both sides of the string;

2. Use the php function: str_replace():

str_replace(' ','',$str)
Copy after login

3. Use the php function: strtr():

strtr($str,array(' '=>''))
Copy after login

4. Use the encapsulation function you wrote:

function trimall($str)//删除空格 { $limit=array(" "," ","\t","\n","\r"); $rep=array("","","","",""); return str_replace($limit,$rep,$str); }
Copy after login

5. Use this regular expression to remove ordinary spaces:

preg_replace('# #', '', $str)
Copy after login

6. Use this regular rule to remove all spaces including full-width spaces:

$str =preg_replace("/\s| /","",$str);
Copy after login

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to delete blank strings in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!