user8240963
user8240963

Reputation:

PHP string function chop() does not give desired output

The following code:

<?php
    $str='Who are you?';
    echo chop($str,'you?').'<br>';
    echo chop($str,'are you?').'<br>';
?>

gives me the output:

Who are
Wh

Why the second output is

Wh

and not

who

Upvotes: 3

Views: 272

Answers (2)

Alex Tartan
Alex Tartan

Reputation: 6836

because:

(PHP 4, PHP 5, PHP 7)
chop — Alias of rtrim()

and

(PHP 4, PHP 5, PHP 7)
rtrim — Strip whitespace (or other characters) from the end of a string

string rtrim ( string $str [, string $character_mask ] )

So... you're feeding a character mask.

Given that, "o" is in that mask, so it gets trimmed out

Upvotes: 8

Marco Luzzara
Marco Luzzara

Reputation: 6056

In your string you have specified a character_mask, this is what reference says:

character_mask: You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

In your case, there's an 'o' in the mask, for this reason all 'o' in $str have been deleted.

Upvotes: 2

Related Questions