Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of PHP built-in ltrim() to remove a single character

Is there a simple way to use ltrim() to remove a single instance of a match instead of all matches?

I'm looping through array of strings and I'd like to remove the first, and only first, match (vowels in this case):

ltrim($value, "aeiouyAEIOUY");

With default behavior the string aardvark or Aardvark would be trimmed to be "rdvark". I'd like result to be "ardvark".

I'm not bound to ltrim by any means but it seemed the closest built-in PHP function. It would be nice of ltrim and rtrim had an optional parameter "limit", just saying... :)

like image 670
ptay Avatar asked Dec 28 '12 22:12

ptay


People also ask

What does trim() do PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.

What is the use of Ltrim and Rtrim in PHP?

The ltrim() function removes whitespace or other predefined characters from the left side of a string. Related functions: rtrim() - Removes whitespace or other predefined characters from the right side of a string. trim() - Removes whitespace or other predefined characters from both sides of a string.

How to remove space and special characters in PHP?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “). Example: This example illustrates the use of the str_replace() function to remove the special characters from the string.

How to remove spaces from a string in PHP?

The trim() function in PHP removes whitespace or any other predefined character from both the left and right sides of a string. ltrim() and rtrim() are used to remove these whitespaces or other characters from the left and right sides of the string.


1 Answers

Just use preg replace it has a limit option

eg

$value = preg_replace('/^[aeiouy]/i', '', $value, 1); 
like image 81
Shaun Hare Avatar answered Sep 26 '22 02:09

Shaun Hare