Search code examples
phpget

PHP Cut At specific character "@"


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);

Solution

  • 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.