Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP ลบช่องว่างส่วนเกินด้านหน้าของสตริง

หากต้องการลบช่องว่างส่วนเกินด้านหน้า (leading whitespace) ให้ใช้ ltrim() ใน PHP สมมติว่ามีตัวแปรดังนี้

$value = "  Hello, welcome to PHP Tutorial  ";

ต้องการผลลัพธ์โดยไม่มีช่องว่างด้านซ้าย

Hello, welcome to PHP Tutorial

ตัวอย่างโค้ด

<!DOCTYPE html> <html> <body> <?php $value = "  Hello, welcome to PHP Tutorial  "; print_r("The actual value is=" . $value . "<br>"); echo "The actual length is=", strlen($value) . "<br>"; $modifiedValue = ltrim($value); echo "After removing extra whitespace from beginning=", strlen($modifiedValue) . "<br>"; echo "The result is=" . $modifiedValue; ?> </body> </html>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้

The actual value is= Hello, welcome to PHP Tutorial The actual length is=34 After removing extra whitespace from beginning=32 The result is=Hello, welcome to PHP Tutorial