PHPMailer::mb_pathinfo PHP Method

mb_pathinfo() public static method

Drop-in replacement for pathinfo(), but multibyte-safe, cross-platform-safe, old-version-safe. Works similarly to the one in PHP >= 5.2.0
public static mb_pathinfo ( string $path, integer | string $options = null ) : string | array
$path string A filename or path, does not need to exist as a file
$options integer | string Either a PATHINFO_* constant, or a string name to return only the specified piece, allows 'filename' to work on PHP < 5.2
return string | array
    public static function mb_pathinfo($path, $options = null)
    {
        $ret = array('dirname' => '', 'basename' => '', 'extension' => '', 'filename' => '');
        $pathinfo = array();
        if (preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\\.([^\\.\\\\/]+?)|))[\\\\/\\.]*$%im', $path, $pathinfo)) {
            if (array_key_exists(1, $pathinfo)) {
                $ret['dirname'] = $pathinfo[1];
            }
            if (array_key_exists(2, $pathinfo)) {
                $ret['basename'] = $pathinfo[2];
            }
            if (array_key_exists(5, $pathinfo)) {
                $ret['extension'] = $pathinfo[5];
            }
            if (array_key_exists(3, $pathinfo)) {
                $ret['filename'] = $pathinfo[3];
            }
        }
        switch ($options) {
            case PATHINFO_DIRNAME:
            case 'dirname':
                return $ret['dirname'];
            case PATHINFO_BASENAME:
            case 'basename':
                return $ret['basename'];
            case PATHINFO_EXTENSION:
            case 'extension':
                return $ret['extension'];
            case PATHINFO_FILENAME:
            case 'filename':
                return $ret['filename'];
            default:
                return $ret;
        }
    }

Usage Example

 /**
  * Miscellaneous calls to improve test coverage and some small tests
  */
 public function testMiscellaneous()
 {
     $this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf'), 'MIME TYPE lookup failed');
     $this->Mail->addCustomHeader('SomeHeader: Some Value');
     $this->Mail->clearCustomHeaders();
     $this->Mail->clearAttachments();
     $this->Mail->isHTML(false);
     $this->Mail->isSMTP();
     $this->Mail->isMail();
     $this->Mail->isSendmail();
     $this->Mail->isQmail();
     $this->Mail->setLanguage('fr');
     $this->Mail->Sender = '';
     $this->Mail->createHeader();
     $this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded');
     $this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed');
     //Test pathinfo
     $a = '/mnt/files/飛兒樂 團光茫.mp3';
     $q = PHPMailer::mb_pathinfo($a);
     $this->assertEquals($q['dirname'], '/mnt/files', 'UNIX dirname not matched');
     $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'UNIX basename not matched');
     $this->assertEquals($q['extension'], 'mp3', 'UNIX extension not matched');
     $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'UNIX filename not matched');
     $this->assertEquals(PHPMailer::mb_pathinfo($a, PATHINFO_DIRNAME), '/mnt/files', 'Dirname path element not matched');
     $this->assertEquals(PHPMailer::mb_pathinfo($a, 'filename'), '飛兒樂 團光茫', 'Filename path element not matched');
     $a = 'c:\\mnt\\files\\飛兒樂 團光茫.mp3';
     $q = PHPMailer::mb_pathinfo($a);
     $this->assertEquals($q['dirname'], 'c:\\mnt\\files', 'Windows dirname not matched');
     $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'Windows basename not matched');
     $this->assertEquals($q['extension'], 'mp3', 'Windows extension not matched');
     $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'Windows filename not matched');
 }
All Usage Examples Of PHPMailer::mb_pathinfo