-2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saravanan M P
  • 561
  • 1
  • 7
  • 12

3 Answers3

4

Try with:

$info = pathinfo('Hello_world.wav');

var_dump($info);

Which gives you:

array (size=4)
  'dirname' => string '.' (length=1)
  'basename' => string 'Hello_world.wav' (length=15)
  'extension' => string 'wav' (length=3)
  'filename' => string 'Hello_world' (length=11)

So:

$a = $info['filename'];
$b = '.' . $info['extension'];
hsz
  • 148,279
  • 62
  • 259
  • 315
0

Split is DEPRECATED as of PHP 5.3.0. You shouldn't use it (PHP manual). Instead, use explode:

$new_str_arr = explode('.', $ab);
$a = $new_str_arr[0];
$b = '.' . $new_str_arr[1];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark
  • 1,376
  • 9
  • 16
0

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;
jerdiggity
  • 3,655
  • 1
  • 29
  • 41