Saravanan M P Saravanan M P - 11 days ago 6
PHP Question

How to separate a string and store in two variables in PHP

I have string

$ab="Hello_world.wav"
, and I want store this string in two variables. One is
$a="Hello_world"
and the other
$b=".wav"
.
How should I use a string function to get it like this?

Answer

You can also use preg_split():

$ab="Hello_world.wav";
$matches = preg_split('/\./', $ab);
$a = isset($matches[0]) ? $matches[0] : '';
$b = isset($matches[1]) ? '.' . $matches[1] : '';
// Print if you want by uncommenting the next line
// print $a . ' ' . $b;