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?
Asked
Active
Viewed 1,460 times
-2

Peter Mortensen
- 30,738
- 21
- 105
- 131

Saravanan M P
- 561
- 1
- 7
- 12
-
3http://stackoverflow.com/questions/10368217/php-get-file-extension – NDM Sep 20 '13 at 07:35
-
What is the desired output if the file name contains additional dots? – Otto Sep 20 '13 at 07:39
-
See also [`pathinfo()`](http://php.net/pathinfo), [`list`](http://php.net/list), [`preg_split()`](http://php.net/preg_split), and other such pages in the quite informative PHP manual. – outis Sep 20 '13 at 07:50
-
if you know tell answer don't put down vote – Saravanan M P Sep 23 '13 at 06:24
3 Answers
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
-
1Why downvote? It seems it's the best solution (I've commented that before) – Alma Do Sep 20 '13 at 07:39
-
-
@jerdiggity It works with string - not existing file. I've tested it without fil in my filesystem. – hsz Sep 20 '13 at 07:44
-
LOL yeah... I realized I said that in the wrong context so I deleted my comment. But yes you're right. – jerdiggity Sep 20 '13 at 07:45
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