regex to find the image name and extension from this url - php

I have this url which is generated automatically.
uploads/1/0/1/7/10178123/4320885_orig.png
I need to get the name and the extension of the image from this url which in this case is 4320885_orig.png
i am not being able to write a regex to find the name and extension.

I recommend you to check php's Basename function instead of using regex
echo basename("uploads/1/0/1/7/10178123/4320885_orig.png");
//result: 4320885_orig.png

Don't use regex, use pathinfo...
echo pathinfo('uploads/1/0/1/7/10178123/4320885_orig.png', PATHINFO_BASENAME);

Try pathinfo():
$path = 'uploads/1/0/1/7/10178123/4320885_orig.png';
echo pathinfo($path, PATHINFO_BASENAME); //png
You can get other components of the path as well:
$path_parts = pathinfo($path);
echo $path_parts['extension'], "\n"; //png
echo $path_parts['dirname'], "\n"; //uploads/1/0/1/7/10178123
echo $path_parts['basename'], "\n"; //4320885_orig.png
echo $path_parts['filename'], "\n"; //4320885_orig
See the PHP documentation on pathinfo() for more information.

Related

How to get Image extention and name when url doesnot have this . (PHP)

I have an URL in following format
www.domain.com/image.php?id=123&idlocation=987&number=01
It has image that has to be downloaded I use following function to download
$codigo = file_get_contents($url);
So now How I can get the exteion of the image file using $codigo
Thanks in advance
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
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
kindly refer this link to get file info
http://php.net/manual/en/function.pathinfo.php

How to get the file path without the file name in php?

For example this is $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] output:
example.com/foldername/subfolder/controller/index.php
I want to end up with example.com/foldername/subfolder/controller/
Thanks..
Check out http://php.net/manual/en/function.pathinfo.php
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
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
?>
The output for that would be
/www/htdocs/inc
lib.inc.php
php
lib.inc
So, in short, you could use $path_parts['dirname'].
print_r( $_SERVER );
http://www.php.net/manual/en/reserved.variables.server.php
http://www.php.net/manual/en/language.constants.predefined.php
PATH_TRANSLATED or __DIR__
Lots of options =)
Add the two together to get
$url = "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Replace string before a string?

Sorry, I'm bad English. I'm going to post my code now:
$image = 'http://example.com/thisisimage.gif';
$filename = substr($image, strrpos($image, '/') + 1);
echo '<br>';
echo $filename;
echo '<br>';
echo preg_replace('/^[^\/]+/', 'http://mydomain.com', $image);
echo '<br>';
$image is string;
$filename is image name (in example above, it returns 'thisisimage.gif')
Now i want replace all before $filename with 'http://mydomain.com', my code is above but it doesnt work.
Thanks!
$foo = explode($filename, $image);
echo $foo[0];
Explode "splits" one the given paramater ( in your case $filename ). It returns an array with where the keys are split on the string you gave.
And if you just want to change the url. you use a str_replace
$foo = str_replace("http://example.com", "http://localhost", $image);
//This will change "http://example.com" to "http://localhost", like a text replace in notepad.
In your case:
$image = 'http://example.com/thisisimage.gif';
$filename = substr($image, strrpos($image, '/') + 1);
$foo = explode($filename, $image);
echo '<br>';
echo $filename;
echo '<br>';
echo str_replace($foo[0], "http://yourdomain.com/", $url);
echo '<br>';
There's another approach in which you don't need a regular expression:
in Short:
$image = 'http://example.com/thisisimage.gif';
$url = "http://mydomain.com/".basename($image);
Explanation:
If you just want the file name without url's or directory path's, basename() is your friend;
$image = 'http://example.com/thisisimage.gif';
$filename = basename($image);
output: thisisimage.gif
Then you can add whatever domain you want:
$mydomain = "http://mydomain.com/";
$url = $mydomain.$filename;
Try this :
$image = 'http://example.com/thisisimage.gif';
echo preg_replace('/^http:\/\/.*\.com/', 'http://mydomain.com',$image);
This should simply work:
$image = 'http://example.com/thisisimage.gif';
$filename = substr($image, strrpos($image, '/') + 1);
echo '<br>';
echo $filename;
echo '<br>';
echo 'http://mydomain.com/'.$filename;
echo '<br>';
if you just like to add your own domain before the file name, try this:
$filename = array_pop(explode("/", $image));
echo "http://mydomain.com/" . $filename;
if you wanna only replace thedomain, try this:
echo preg_replace('/.*?[^\/]\/(?!\/)/', 'http://mydomain.com/', $image);
The other people here have given good answers about how to do it - regex has its advantages but also drawbacks - its slower, respectively requires more resources and for something simple as this, I would advice you to use the explode approach, but while speaking for regex functions you also may try this, instead your preg_replace:
echo preg_replace('#(?:.*?)/([^/]+)$#i', 'http://localhost/$1', $image);
It seems variable length positve lookbehind is not supported in PHP.

How to get dir name from full path with PHP?

I have a image path like this..
2012/12/14/example.jpg
Now I would like to get the path except image name..
I mean I need like this
2012/12/14/
Can anyone help me? Thanks
I guess this is what you need
<?php
$path_parts = pathinfo('2012/12/14/example.jpg');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n";
?>
output will be..
/www/htdocs/inc
example.jpg
jpg
example

removing last 3 characters on a file (file extension)

my file name are being stored in a variable $file_name... how can i remove the extension and just have the name only? is there something other than strcmp that i can use... that doesn't seem to do it
Use pathinfo.
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
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
?>
Note: If you're not on PHP >= 5.2 use this to compose the filename
$path_parts['filename'] = substr($path_parts['basename'], 0, -1*strlen($path_parts['extension'])-1);
You can do:
$file_name_no_ext = pathinfo($file_name , PATHINFO_FILENAME);
substr($file_name, 0, -4);
You could use regular expressions. Regular expression to remove a file's extension
You should take a look at the pathinfo() function.
Similar to #fire, but more robust in the face of multiple dots:
$comps = explode('.', $filename);
unset($comps[count($comps) - 1]);
$ext = implode('.', $comps);
I use:
$file_name = current(explode(".", $file_name));

Categories