Catch last delimiter with explode

Asked

Viewed 676 times

4

I’m wanting to grab a file extension but the name of some images comes type:

adsfasdfasd.234.asdfa.3rfr.jpg

asdfsdafasdfasdf.45eg.png

I’m trying to use the code:

<?php
if(is_dir("img/$noticia->idnoticia"))
{
$diretorio = "img/$noticia->idnoticia/";
if ($handle = opendir($diretorio)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
list($arquivo, $ext) = explode(strrchr($file,"."),$file);
if(($ext!="mp3")AND($ext!="wav")){
echo "<li><img src='$diretorio$arquivo.$ext'></li>";

But the result is being ..

any hint?

2 answers

5


You can use the function pathinfo with the option PATHINFO_EXTENSION to obtain exclusively that information.

$string = "adsfasdfasd.234.asdfa.3rfr.jpg";
echo pathinfo($string, PATHINFO_EXTENSION); // jpg

DEMO

Your code should look like this:

$diretório = getcwd(); // Diretório atual

if ($handle = opendir($diretório)) {
    while (false !== ($file = readdir($handle))) {
        if (is_file($file) && $file != "." && $file != "..") {
            $info = pathinfo($file);
            $arquivo = $info['filename']; // Nome do arquivo
            $ext = $info['extension'];    // Extensão
        }
    }
    closedir($handle); // Fecha o manipulador aberto por opendir()
}
  • I edited the question with the changes that suggested however now only returns = .

  • 1

    @Arsomnolasco Is that you are not passing the name of a file instead of the directory in the variable diretorio? take a look at this commenting from the PHP website.

  • the problem is that I also need to list everything get the name of the separate file posted the complete code

  • 1

    @Arsomnolasco I typed wrong the previous message.. initial problem still persists? have a look at the current answer code.

  • 1

    yes yes it worked, somehow I found that using the LIST was mandatory, however it seems to me that the pathinfo function already lists, obr

3

Complementing what has already been answered previously, there are some alternatives to pathinfo, only to show other means of working with strings, given the high performance in the case of such simple operations.


Getting only the extension:

I didn’t use list combined with explode because it does not function correctly in the case of several points in the middle of the name.

Using explodes:

$ext = end( explode( '.', $file) );

Using string operations (preferable):

$ext = strrchr( $file, '.' ); // retorna do último ponto em diante .jpg

also:

$ext = substr( strrchr( $file, '.' ), 1); // retorna jpg (sem o ponto)

or even:

$pos = strrpos( $file, '.' );
$ext = ( $pos === false ) ? '' : substr( $file, $pos + 1 );


Getting extension and file name:

$pos = strrpos( $file, '.' );
$ext = ( $pos === false ) ? '' : substr( $file, $pos + 1 );
$arq = ( $pos === false ) ? $file : substr( $file, 0, $pos );


See working on IDEONE.

  • more like listing ? list($file) = end( explode( '.', $file) ); << does not work

  • 1

    The problem with listing is that you wouldn’t take the whole name of a file if it came with two dots, so I didn’t put example with list. You would have to use Reverse in the array, something like that: list( $ext, $arquivo ) = array_reverse( explode( '.', $file ) ). Notice that I reversed ext’s position, and as I said, if you have um.arquivo.jpg the "one" will be left out. Explode and list are not suitable for this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.