0

I have this code at the moment in php the GET Receives words from a variable and the vast majority starts with a "@" then I add a "substr" to omit the first character which is "@" but there is a time when I get words without the @, is there some way to omit only the @? I would very much appreciate the help thank you very much!!

$soe = $_GET["option"];
$so = substr($soe, 1);

2 Answers2

0

Just check if the string begins with @

$so = $soe[0] == "@" ? substr($soe, 1) : $soe;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Just trim the @ character from the left. If it's there it's removed, if not then it stays the same:

$so = ltrim($soe, '@');

If you run in to a situation where you want to remove multiple characters, then you can specify them or a range:

$so = ltrim($soe, '@#');
// or
$so = ltrim($soe, '0..9'); // all numbers 0 through 9

You can do the same with rtrim for the right or trim for both ends.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Thank you so much for the help! I can ask you something extra... sorry D:, but I have an array with some words that if they match the word they receive from the GET they print a message, but sometimes it doesn't work because it doesn't match the capitals, is there any way to ignore the capitals and minicules? :( and sorry I'm very new to php $channel = array( "AnyAny" => $prefix. "", – Hemerson Osorio Jun 25 '21 at 22:30
  • Ask that as a new question with code and what you want to match. – AbraCadaver Jun 25 '21 at 22:55
  • 1
    I never knew about the `0..9` range feature. – Barmar Jun 26 '21 at 19:50
  • You can even use multiple ranges in the character mask. https://stackoverflow.com/a/65322208/2943403 – mickmackusa Mar 12 '22 at 14:22