Extracting string from file address - php

I have string like "/files/temp/ABCDxyz-dfgdf.jpg"
I need to extract ABCD.
First I'm trying to get the whole filename with this
$file="/files/temp/ABCDxyz-dfgdf.jpg";
$fileName=explode("files/temp/",$file)[0];
But it is not working.

Use basename to get the filename without the directory:
$filename = basename($file); // => ABCDxyz-dfgdf.jpg
$beginning = substr($filename, 0, 4); // => ABCD

Are you looking for the first four caracters of the filename ?
If you have a path :
$path = '/files/temp/ABCDxyz-dfgdf.jpg';
You can get the filename with substr :
$filename = basename($path);
And then extract the four first chars :
$firstFour = substr($filename,0,4);
echo $firstFour;

The below piece of code results in
ABCDxyz-dfgdf.jpg
you can go ahead from here on extracting ABCD
$name = "/files/temp/ABCDxyz-dfgdf.jpg";
$temp = explode("/files/temp/",$name);
print "\n".$temp[1]; // give out ABCDxyz-dfgdf.jpg

You can use this regex to get first four characters after path
$str = "/files/temp/ABCDxyz-dfgdf.jpg";
preg_match_all('/files\/temp\/([a-z]{4})/i',$str,$m);
echo $m[1][0];

Try with this code:
<?php
// your file
$file = '/files/temp/ABCDxyz-dfgdf.jpg';
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
echo $file_name;
$firstFour = substr($file_name,0,4);
echo $firstFour;
?>
Output:-
Your File path:- ABCDxyz-dfgdf
REQUIRED OUTPUT: ABCD
reference Example : Cilck Here

Related

I want to retrieve the filename without extension from an url with php

I have an url which contains various POST-DATA in it And an image file at last.
My link is : http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png
I want to seperate the 565dbca63791e5.87676354.png from the url and seperate the extension (.png) from it.
I can do it :but it is from only plain URL:
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
$filename = $_REQUEST['signature'];
$pathinfo = pathinfo($filename);
$pathinfo['filename'] will print 565dbca63791e5.87676354 and $pathinfo['extension'] will be png.
First of all: use parse_url() as suggested in the comments. If you however opt for a regex solution, consider the following code:
$str = "http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png";
$regex = "/signature=(?<signature>[^&]+)/";
// that is: match signature= literally, then match everything up to a new ampersand and save it to the group "signature"
preg_match($regex, $str, $matches);
$signature = $matches["signature"];
$ext = substr(strrchr($signature, '.'), 1);
print "Signature: $signature with extension: $ext";
// prints out: Signature: 565dbca63791e5.87676354.png with extension: png
See a working PHP fiddle here.
If i correctly understand you, then you need some function like this
$arr = explode('.', $_REQUEST['signature']);
function arrayFilter($arr){
foreach($arr as $key=>$item){
if(!next($arr)){
$result['extension'] = $item;
} else {
$result['value'] .= $item . '.';
}
}
$result['value'] = substr($result['value'], 0, -1);
return $result;
}
$data= arrayFilter($arr);
And will print
[value] => 565dbca63791e5.87676354 [extension] => png

Return first part of filename and not the file extension

I have a file: ./image/photo.jpg
I was wondering if there is a php function that returns just the filename without the extension without the use of regular expression.
I have used some functions but it returns photo.jpg
http://php.net/manual/de/function.basename.php
<?php
$string = './image/photo.jpg';
$name = basename($string, '.jpg');
// $name will now contain "photo"
or http://php.net/manual/de/function.pathinfo.php
<?php
$string = './image/photo.jpg';
$info = pathinfo($string);
// $info['filename'] will now contain "photo"
pathinfo:
$string = './image/photo.jpg';
echo pathinfo($string, PATHINFO_FILENAME); // output: photo

Get only filename from url in php without any variable values which exist in the url

I want to get filename without any $_GET variable values from a URL in php?
My URL is http://learner.com/learningphp.php?lid=1348
I only want to retrieve the learningphp.php from the URL?
How to do this?
I used basename() function but it gives all the variable values also: learntolearn.php?lid=1348 which are in the URL.
This should work:
echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);
But beware of any malicious parts in your URL.
Following steps shows total information about how to get file, file with extension, file without extension. This technique is very helpful for me. Hope it will be helpful to you too.
$url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
$file = file_get_contents($url); // to get file
$name = basename($url); // to get file name
$ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
$name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
Is better to use parse_url to retrieve only the path, and then getting only the filename with the basename. This way we also avoid query parameters.
<?php
// url to inspect
$url = 'http://www.example.com/image.jpg?q=6574&t=987';
// parsed path
$path = parse_url($url, PHP_URL_PATH);
// extracted basename
echo basename($path);
?>
Is somewhat similar to Sultan answer excepting that I'm using component parse_url parameter, to obtain only the path.
Use parse_url() as Pekka said:
<?php
$url = 'http://www.example.com/search.php?arg1=arg2';
$parts = parse_url($url);
$str = $parts['scheme'].'://'.$parts['host'].$parts['path'];
echo $str;
?>
http://codepad.org/NBBf4yTB
In this example the optional username and password aren't output!
Your URL:
$url = 'http://learner.com/learningphp.php?lid=1348';
$file_name = basename(parse_url($url, PHP_URL_PATH));
echo $file_name;
output: learningphp.php
You can use,
$directoryURI =basename($_SERVER['SCRIPT_NAME']);
echo $directoryURI;
An other way to get only the filename without querystring is by using parse_url and basename functions :
$parts = parse_url("http://example.com/foo/bar/baz/file.php?a=b&c=d");
$filename = basename($parts["path"]); // this will return 'file.php'
Try the following code:
For PHP 5.4.0 and above:
$filename = basename(parse_url('http://learner.com/learningphp.php?lid=1348')['path']);
For PHP Version < 5.4.0
$parsed = parse_url('http://learner.com/learningphp.php?lid=1348');
$filename = basename($parsed['path']);
$filename = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
Use parse_url to extract the path from the URL, then pathinfo returns the filename from the path
The answer there assumes you know that the URL is coming from a request, which it may very well not be. The generalized answer would be something like:
$basenameWithoutParameters = explode('?', pathinfo($yourURL, PATHINFO_BASENAME))[0];
Here it just takes the base path, and splits out and ignores anything ? and after.
$url = "learner.com/learningphp.php?lid=1348";
$l = parse_url($url);
print_r(stristr($l['path'], "/"));
Use this function:
function getScriptName()
{
$filename = baseName($_SERVER['REQUEST_URI']);
$ipos = strpos($filename, "?");
if ( !($ipos === false) ) $filename = substr($filename, 0, $ipos);
return $filename;
}
May be i am late
$e = explode("?",basename($_SERVER['REQUEST_URI']));
$filename = $e[0];

How do I use PHP to grab the name of the file?

what I want to do is PHP to look at the url and just grab the name of the file, without me needing to enter a path or anything (which would be dynamic anyway). E.G.
http://google.com/info/hello.php, I want to get the 'hello' bit.
Help?
Thanks.
You need basename and explode to get name without extension:
$name = basename($_SERVER['REQUEST_URI']);
$name_array = explode('.', $name);
echo $name_array[0];
$filename = __FILE__;
Now you can split this on the dot, for example
$filenameChunks = split(".", $filename);
$nameOfFileWithoutDotPHP = $filenameChunks[0];
This is safe way to easily grab the filename without extension
$info = pathinfo(__FILE__);
$filename = $info['filename'];
$_SERVER['REQUEST_URI'] contains the requested URI path and query. You can then use parse_url to get the path and basename to get just the file name:
basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '.php')
http://php.net/manual/en/function.basename.php
$file = basename(__FILE__); // hello.php
$file = explode('.',$file); // array
unset($file[count($file)-1]); // unset array key that has file extension
$file = implode('.',$file); // implode the pieces back together
echo $file; // hello
You could to this with parse_url combined with pathinfo
Here's an example
$parseResult = parse_url('http://google.com/info/hello.php');
$result = pathinfo($parseResult['path'], PATHINFO_FILENAME);
$result will contain "hello"
More info on the functions can be found here:
parse_url
pathinfo

How to remove extension from string (only real extension!)

I'm looking for a small function that allows me to remove the extension from a filename.
I've found many examples by googling, but they are bad, because they just remove part of the string with "." . They use dot for limiter and just cut string.
Look at these scripts,
$from = preg_replace('/\.[^.]+$/','',$from);
or
$from=substr($from, 0, (strlen ($from)) - (strlen (strrchr($filename,'.'))));
When we add the string like this:
This.is example of somestring
It will return only "This"...
The extension can have 3 or 4 characters, so we have to check if dot is on 4 or 5 position, and then remove it.
How can it be done?
http://php.net/manual/en/function.pathinfo.php
pathinfo — Returns information about a file path
$filename = pathinfo('filename.md.txt', PATHINFO_FILENAME); // returns 'filename.md'
Try this one:
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
So, this matches a dot followed by three or four characters which are not a dot or a space. The "3 or 4" rule should probably be relaxed, since there are plenty of file extensions which are shorter or longer.
From the manual, pathinfo:
<?php
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // Since PHP 5.2.0
?>
It doesn't have to be a complete path to operate properly. It will just as happily parse file.jpg as /path/to/my/file.jpg.
Use PHP basename()
(PHP 4, PHP 5)
var_dump(basename('test.php', '.php'));
Outputs: string(4) "test"
This is a rather easy solution and will work no matter how long the extension or how many dots or other characters are in the string.
$filename = "abc.def.jpg";
$newFileName = substr($filename, 0 , (strrpos($filename, ".")));
//$newFileName will now be abc.def
Basically this just looks for the last occurrence of . and then uses substring to retrieve all the characters up to that point.
It's similar to one of your googled examples but simpler, faster and easier than regular expressions and the other examples. Well imo anyway. Hope it helps someone.
Recommend use: pathinfo with PATHINFO_FILENAME
$filename = 'abc_123_filename.html';
$without_extension = pathinfo($filename, PATHINFO_FILENAME);
You could use what PHP has built in to assist...
$withoutExt = pathinfo($path, PATHINFO_DIRNAME) . '/' . pathinfo($path, PATHINFO_FILENAME);
Though if you are only dealing with a filename (.somefile.jpg), you will get...
./somefile
See it on CodePad.org
Or use a regex...
$withoutExt = preg_replace('/\.' . preg_quote(pathinfo($path, PATHINFO_EXTENSION), '/') . '$/', '', $path);
See it on CodePad.org
If you don't have a path, but just a filename, this will work and be much terser...
$withoutExt = pathinfo($path, PATHINFO_FILENAME);
See it on CodePad.org
Of course, these both just look for the last period (.).
The following code works well for me, and it's pretty short. It just breaks the file up into an array delimited by dots, deletes the last element (which is hypothetically the extension), and reforms the array with the dots again.
$filebroken = explode( '.', $filename);
$extension = array_pop($filebroken);
$fileTypeless = implode('.', $filebroken);
I found many examples on the Google but there are bad because just remove part of string with "."
Actually that is absolutely the correct thing to do. Go ahead and use that.
The file extension is everything after the last dot, and there is no requirement for a file extension to be any particular number of characters. Even talking only about Windows, it already comes with file extensions that don't fit 3-4 characters, such as eg. .manifest.
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 havent 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'];
// Or in PHP 5.4, i believe this should work
$name = pathinfo( $filename )[ 'filename' ];
In all of these, $name contains the filename without the extension
$image_name = "this-is.file.name.jpg";
$last_dot_index = strrpos($image_name, ".");
$without_extention = substr($image_name, 0, $last_dot_index);
Output:
this-is.file.name
As others mention, the idea of limiting extension to a certain number of characters is invalid. Going with the idea of array_pop, thinking of a delimited string as an array, this function has been useful to me...
function string_pop($string, $delimiter){
$a = explode($delimiter, $string);
array_pop($a);
return implode($delimiter, $a);
}
Usage:
$filename = "pic.of.my.house.jpeg";
$name = string_pop($filename, '.');
echo $name;
Outputs:
pic.of.my.house (note it leaves valid, non-extension "." characters alone)
In action:
http://sandbox.onlinephpfunctions.com/code/5d12a96ea548f696bd097e2986b22de7628314a0
This works when there is multiple parts to an extension and is both short and efficient:
function removeExt($path)
{
$basename = basename($path);
return strpos($basename, '.') === false ? $path : substr($path, 0, - strlen($basename) + strlen(explode('.', $basename)[0]));
}
echo removeExt('https://example.com/file.php');
// https://example.com/file
echo removeExt('https://example.com/file.tar.gz');
// https://example.com/file
echo removeExt('file.tar.gz');
// file
echo removeExt('file');
// file
You can set the length of the regular expression pattern by using the {x,y} operator. {3,4} would match if the preceeding pattern occurs 3 or 4 times.
But I don't think you really need it. What will you do with a file named "This.is"?
Landed on this page for looking for the fastest way to remove the extension from a number file names from a glob() result.
So I did some very rudimentary benchmark tests and found this was the quickest method. It was less than half the time of preg_replace():
$result = substr($fileName,0,-4);
Now I know that all of the files in my glob() have a .zip extension, so I could do this.
If the file extension is unknown with an unknown length, the following method will work and is still about 20% faster that preg_replace(). That is, so long as there is an extension.
$result = substr($fileName,0,strrpos($fileName,'.'));
The basic benchmark test code and the results:
$start = microtime(true);
$loop = 10000000;
$fileName = 'a.LONG-filename_forTest.zip';
$result;
// 1.82sec preg_replace() unknown ext
//do {
// $result = preg_replace('/\\.[^.\\s]{3,4}$/','',$fileName);
//} while(--$loop);
// 1.7sec preg_replace() known ext
//do {
// $result = preg_replace('/.zip$/','',$fileName);
//} while(--$loop);
// 4.57sec! - pathinfo
//do {
// $result = pathinfo($fileName,PATHINFO_FILENAME);
//} while(--$loop);
// 2.43sec explode and implode
//do {
// $result = implode('.',explode('.',$fileName,-1));
//} while(--$loop);
// 3.74sec basename, known ext
//do {
// $result = basename($fileName,'.zip');
//} while(--$loop);
// 1.45sec strpos unknown ext
//do {
// $result = substr($fileName,0,strrpos($fileName,'.'));
//} while(--$loop);
// 0.73sec strpos - known ext length
do {
$result = substr($fileName,0,-4);
} while(--$loop);
var_dump($fileName);
var_dump($result);
echo 'Time:['.(microtime(true) - $start).']';
exit;
Use this:
strstr('filename.ext','.',true);
//result filename
Try to use this one. it will surely remove the file extension.
$filename = "image.jpg";
$e = explode(".", $filename);
foreach($e as $key=>$d)
{
if($d!=end($e)
{
$new_d[]=$d;
}
}
echo implode("-",$new_t); // result would be just the 'image'
EDIT:
The smartest approach IMHO, it removes the last point and following text from a filename (aka the extension):
$name = basename($filename, '.' . end(explode('.', $filename)));
Cheers ;)

Categories