PHP Ltrim Behavior with Character List

PHP ltrim behavior with character list

The second argument to ltrim is a list of characters to remove from the left side of the string.

If you did

<?php
ltrim('lllliiiiaaaaaatttttt', 'mailto:');
?>

You would get an empty string as the return value.

Try this instead:

<?php
$email = 'mailto:bob@example.com';
$fixedEmail = substr($email, 0, 7) == 'mailto:' ? substr($email, 7) : $email;
?>

Why does ltrim remove one character when the second argument contains an operator sign?

ltrim('53-34567', '53-');

There is a 5 at the begining of '53-34567' so it is removed.

There is a 3 at the begining of '3-34567' so it is removed.

There is a - at the begining of '-34567' so it is removed.

There is a 3 at the begining of '34567' so it is removed.

There is nothing in '53-' at the begining of '4567' so it stopped.

This is the same behaviour than a trim() by removing unwanted trailing characters. In example, trim(" lots of spaces "); will return "lots of spaces" by removing trailing and leading spaces but will keep the inner ones.

Strange behaviour from ltrim() with forward slash

ltrim removes ALL characters found, consider the following:

$test = 'price/edit.php';
echo ltrim($test, 'dprice/'); // outputs t.php

For this particular scenario, you should probably be using str_replace.

Trim multiple characters using php

Use the optional second argument to trim which allows you to specify the list of characters to trim:

<?php
$str = "{Hello {W}orld}";
$str = trim($str, "{}");

echo "Trimmed: $str";

Output:

Trimmed: Hello {W}orld

How to prevent some characters in trim function in php

Very well, I'll bite. What you need to use is a negative lookbehind in a regular expression. The expression, in it's simplest form would look like this:

/(?<!\\)"/

Breakdown:

  • (?<!: Negative lookbehind. This means that you want to check that the expression is not preceded by a certain pattern:
  • \\): The lookbehind pattern. In this case, the pattern will only match if it's not preceded by a literal \.
  • ": A literal ". Again, because of the lookbehind, you won't match double quotes that are escaped.

demo

In your case, you're probably looking for:

$str = preg_replace('/(?<!\\)"/', '', $str);

Replacing all " that are not escaped with an empty string (essentially removing them).


Because you're using trim, you might only want to remove leading and trailing quotes. In that case, you'll have to alter the expression just a tiny bit, and use this:

/(?<!\\)(^"|"$)/

Breakdown:

  • (?<!\\): same as before (negative lookbehind)
  • (^"|"$): still matching a literal ", but only if it's the start or the end of the string.

Of course, you don't really need the lookbehind for the leading quote, so this expression will work just as good (if not better):

/(^"|(?<!\\)"$)/

Breakdown:

  • ^": Matches the first character in the string if it is an unescaped ".
  • (?<!\\): same old negative Lookbehind
  • "$: Matches a double quote at the end of the string (only if it's not preceded by a \, as per negative lookbehind

Both of these regex's will allow you to process strings like this:

$str = '"foo"bar\""';
echo preg_replace('/(^"|(?<!\\)"$)/', '', $str);

output foo"bar\"

Demo II

PHP ltrim() not working as expected

Why not do the simple str_replace() ?

$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg

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

Just use preg replace it has a limit option

eg

$value = preg_replace('/^[aeiouy]/i', '', $value, 1); 

Trim only the first and last occurrence of a character in a string (PHP)

First thing on my mind:

if ($string[0] == '/') $string = substr($string,1);
if ($string[strlen($string)-1] == '/') $string = substr($string,0,strlen($string)-1);


Related Topics



Leave a reply



Submit