Search code examples
phpfunctionphp-5.2strstr

PHP strstr() function replacement


Here is an example of how I am using strstr on my localhost on PHP 5.3.10

<?php
$string  = '25_testing';
$test = strstr($string, '_', true); // As of PHP 5.3.0
echo $test; // prints 25
?>

Well, I uploaded my files on my hosting server but they are running off of PHP 5.2 so the function strstr($string, '_', true) does not work. Is there an alternative I can use, to get the same results?


Solution

  • Try this...

    First you return the string after "_" (including itself) and then replace it with nothing. Not very nice but it works ;)

    <?php
        $string = "25_testing";
        echo str_replace(stristr($string,"_"),"",$string);
    ?>
    

    or

    <?php
        $string = "25_testing";
        echo str_replace(strstr($string,"_"),"",$string);
    ?>