This question already has answers here:
How to get a file's extension in PHP?
(31 answers)
Closed 9 years ago.
I have string "katrina.bhuvnesh.jpg" , I want to get jpg that means substring that matches the last dot(.)
I looked in substr() function but it's not working exactly what I need.
Please give me any function of PHP that matches string from last and returns substring from that point.
Thanks!!
Use pathinfo() like this:
$extension = pathinfo("katrina.bhuvnesh.jpg", PATHINFO_EXTENSION);
You can use 2 function calls:
$filename = 'somefile.sometext.jpg';
$type = end(explode('.', $filename));
// $type is now 'jpg'
Alternatively, you can use substr:
$type = substr($filename, strrpos($filename, '.'));
Related
This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 2 years ago.
i have file names strings like this :
Admin_studies.php
Studies_lang.php
and i want to replace it like this :
Admin_classes.php
Classes_lang.php
which keep the case as it's and just replace the sting word
i tried something like this , but it will not keep the original chars cases
$newname = "classes";
$oldname = "studies";
$file_name = "Admin_studies.php"; //or may be $file_name = "Studies_lang.php";
echo preg_replace("/($oldname)/i","$1",$file_name );
You can set arrays for replacement templates
$newnames = ['classes', 'Classes'];
$oldnames = ['studies', 'Studies'];
$file_name = "Admin_Studies.php";
echo str_replace( $oldnames, $newnames, $file_name );
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
$fileName = "Backup_1486874341.tar.gz"
Here is my file name. How can i get only timestamp from that file name Like 1486874341
Use simple regex in preg_match() to select target digit in file name.
$fileName = "Backup_1486874341.tar.gz";
preg_match("/_(\d+)./", $fileName, $matches);
echo $matches[1];
See result in demo
Here is the sample snippet:
<?php
$str = 'Backup_1486874341.tar.gz';
preg_match_all('!\d+!', $str, $matches);
print_r($matches[0][0]);
?>
This question already has answers here:
Extract domain from url (including the hard ones) [duplicate]
(3 answers)
Closed 9 years ago.
I want to get the domain extension from the url. eg. .com, .net etc.
I have used this:
$extension = pathinfo($_SERVER['SERVER_NAME'], PATHINFO_EXTENSION);
I was just wondering if there was a better way using parse_url?
Use parse_url(). Something like:
$host = parse_url('http://www.google.co.uk/test.html');
preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i', $host['host'], $m);
$ext = isset($m[2]) ? $m[2]: '';
Edit: Fixed with regex from this answer. Extension like .co.uk are supported too.
You can easily split the requested URI like this:
function tld( $uri ) {
$parts = explode('.', $uri);
return (sizeof($parts) ? ('.' . end($parts)) : false;
}
So you can do this:
if(!tld('googlecom')) // does not contain an ending TLD
if(tld('google.com')) // this is a valid domain name; output: ".com"
Or you can just use:
echo $_SERVER['SERVER_NAME'];
So in my oppinion the most best example of usage is:
echo tld($_SERVER['SERVER_NAME']);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How remove extension from string (only real extension!)
I am brand new to php and have a lot to learn! I'm experimenting with MiniGal Nano for our King County Iris Society website. It work quite well for our purposes with one small exception: the photo file name needs to be visible under the thumbnail. I've created a work around, but it shows the extension. I've found code samples of functions but have no idea how to incorporate them into the existing code. Any assistance would be greatly appreciated.
Example link: http://www.kcis.org/kcisphotogallery.php?dir=Iris.Japanese
Many thanks!
There are a few ways to do it, but i think one of the quicker ways is the following
// $filename has the file name you have under the picture
$temp = explode('.', $filename);
$ext = array_pop($temp);
$name = implode('.', $temp);
Another solution is this. I haven't tested it, but it looks like it should work for multiple periods in a filename
$name = substr($filename, 0, (strlen($filename))-(strlen(strrchr($filename, '.'))));
Also:
$info = pathinfo($filename);
$name = $info['filename'];
$ext = $info['extension'];
// Shorter
$name = pathinfo($file, PATHINFO_FILENAME);
// Or in PHP 5.4
$name = pathinfo($filename)['filename'];
In all of these, $name contains the filename without the extension
You can use pathinfo() for that.
<?php
// your file
$file = 'image.jpg';
$info = pathinfo($file);
// from PHP 5.2.0 :
$file_name = $info['filename'];
// before PHP 5.2.0 :
// $file_name = basename($file,'.'.$info['extension']);
echo $file_name; // outputs 'image'
?>
If you know for certain that the file extension is always going to be four characters long (e.g. ".jpg"), you can simply use substr() where you output the filename:
echo substr($filename, 0, -4);
If there's a chance that there will be images with more or less characters in the file extension (e.g. ".jpeg"), you will need to find out where the last period is. Since you're outputting the filename from the first character, that period's position can be used to indicate the number of characters you want to display:
$period_position = strrpos($filename, ".");
echo substr($filename, 0, $period_position);
For information about these functions, check out the PHP manual at http://php.net/substr and http://php.net/strrpos.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to extract a file extension in PHP?
I have a variable $filename="filename.ext" or $filename="filena.m.e.ext" or so on.. How can i extract the extension (here ext) from the variable / string? The variable may change or may have more than one dots.. In that case, i want to get the part after the last dot..
see the answer :
$ext = pathinfo($filename, PATHINFO_EXTENSION);
You can use the path info interrogation.
$info = pathinfo($file);
where
$info['extension']
contains the extension
you could define a function like this:
function get_file_extension($filename)
{
/*
* "." for extension should be available and not be the first character
* so position should not be false or 0.
*/
$lastDotPos = strrpos($fileName, '.');
if ( !$lastDotPos ) return false;
return substr($fileName, $lastDotPos+1);
}
or you could use the Spl_FileInfo object built into PHP
You want to use a regular expression:
preg_match('/\.([^\.]+)$/', $filename);
You can test it out here to see if it gets you the result you want given your input.
There are many ways to do this, ie with explode() or with a preg_match and others.
But the way I do this is with pathinfo:
$path_info = pathinfo($filename);
echo $path_info['extension'], "\n";
You could explode the string using ., then take the last array item:
$filename = "file.m.e.ext";
$filenameitems = explode(".", $filename);
echo $filenameitems[count($filenameitems) - 1]; // .ext
// or echo $filenameitem[-1];