PHP TRIM SPACES BEFORE AND AFTER CODE EXAMPLE, PHP TRIM SPACES BEFORE AND AFTER

I have two words spirited by space of course, and a lot of spaces before và after,what I need to vì chưng is to remove the before and after spaces without the in between once.

Bạn đang xem: Php trim spaces before and after code example, php trim spaces before and after

How can I remove the spaces before & after it?


*

*

Trending sort Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to lớn surface more up-to-date answers.

It falls back khổng lồ sorting by highest score if no posts are trending.

Switch khổng lồ Trending sort
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
You don"t need regex for that, use trim():

$words = " my words ";$words = trim($words);var_dump($words);// string(8) "my words"This function returns a string with whitespace stripped from the beginning & end of str.

Xem thêm: Đơn Vị Oz Là Gì ? Cách Đổi Oz Sang Ml Thông Dụng Đơn Vị Oz Là Gì


*

For completeness (as this question is tagged regex), here is a trim() reimplementation in regex:

function preg_trim($subject) $regex = "/s*(.*)s*/s"; if (preg_match ($regex, $subject, $matches)) $subject = $matches<1>; return $subject;$words = " my words ";$words = preg_trim($words);var_dump($words);// string(8) "my words"

*

For some reason two solutions above didnt worked for me, so i came up with this solution.

function cleanSpaces($string) while(substr($string, 0,1)==" ") $string = substr($string, 1); cleanSpaces($string); while(substr($string, -1)==" ") $string = substr($string, 0, -1); cleanSpaces($string); return $string;

*

The question was about how to bởi vì it with regex, so:

$str1=~ s/^s+|s+$//g; That says ^ at the begining s+ (white space) | or s+$ (whitespace at the end) //g remove repeatedly. This same concept works in "ed" (vi/vim)

sometimes it is better just khổng lồ answer the question that was asked.


If trim is not working for you as well , try this. You just need to lớn write it khổng lồ a different variable;

$str = " hello world ";echo strlen($str);$trim_str = trim($str);echo strlen($trim_str);
Thanks for contributing an answer to lớn Stack Overflow!

Please be sure khổng lồ answer the question. Provide details & share your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy & cookie policy


Not the answer you're looking for? Browse other questions tagged php regex or ask your own question.


Site design / hình ảnh sản phẩm © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2022.8.12.42806


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.