phpstringexplodedelimited

Split a string on a delimiting string to create two variables


I have an alphanumeric string like 1234and5678. I want to store the numbers preceding and i.e 1234 into one variable and the number after and i.e 5678 into another variable as shown below:

$var1 = 1234;
$var2 = 5678;

also what should do if I replace and by some random special characters like #$% etc.


Solution

  • $string = "1234and5678";
    list($before, $after) = explode("and", $string);
    

    This splits the string into two variables based on the delimeter ("and"), whatever is before and is saved in a variable called $before, whatever is after is saved into a variable called $after