Method: 1. Use the mb_ereg_replace function to remove, the syntax format is "mb_ereg_replace(regular expression, '', $str)"; 2. Use the trim function to remove whitespace characters on both sides of the string, the syntax format is is "rtrim(string," ")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The first method: through php’s own Function
trim() function removes whitespace characters or other predefined characters on both sides of a string.
<?php $str = "Hello World!"; echo $str . "<br>"; echo trim($str,"Hed!"); ?>
The second method: replace by regular expression, more powerful
php removes spaces at the beginning and end of the string (including full-width)
<?php $str=" hello world "; $str = mb_ereg_replace('^( | )+', '', $str); $str = mb_ereg_replace('( | )+$', '', $str); echo mb_ereg_replace(' ', "\n ", $str); ?>
Recommended: " Summary of PHP interview questions in 2021 (collection) 》《php video tutorial》
The above is the detailed content of How to remove spaces on both sides in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
