Search code examples
phpdrupaldrupal-7

How i can separate a sentence in php?


I have a sentence with [1234]-blablabla and i want to keep only the number 1234. Could someone tell how could I achieve this thing?


Solution

  • Try this:-

    $str = '[1234]-blablabla';
    preg_match_all('!\d+!', $str, $matches);
    echo "<pre/>";print_r($matches); // print as an array
    $new_string = $matches[0][0]; // assign to an string variable
    
    echo $new_string; // print that string variable.
    

    Output:- http://prntscr.com/7ano0t

    Note:- it's up to you how you want. Array or string. I putted both for your convenience. thanks.