Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove first and last spaces in php [closed]

Tags:

string

php

 $search = substr($search_string, 0, -1);

It is only removing last one space only. Some times in last character after having 2 or more spaces how to remove all spaces after word last character and if some times end user entered first character space how to remove also first character?

like image 326
user2750450 Avatar asked Dec 25 '22 20:12

user2750450


2 Answers

You could use trim:

   $s = "  blabla   ";
   $s = trim($s, " ");
like image 135
Bart Friederichs Avatar answered Jan 09 '23 03:01

Bart Friederichs


use trim():

trim — Strip whitespace (or other characters) from the beginning and end of a string

$search_string = trim($search_string);
like image 41
Jason OOO Avatar answered Jan 09 '23 04:01

Jason OOO