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... :)
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.
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.
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.
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.
Just use preg replace it has a limit option
eg
$value = preg_replace('/^[aeiouy]/i', '', $value, 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With