简体   繁体   中英

preg_match and images URL

I have a little problem with preg_match function in PHP. I think that I never will learn how to use this function. I want to extract URL of image from HTML without name of image. For example, if I have some link for image:

"/data/images/2013-10-03/someimage.jpg"

or

"http://something.com//data/images/2013-10-03/someimage.jpg"

How can I use preg_match function to delete everything left of last forward slash, so I can get only image name from URL?

Maybe it's smarter to use different function but I dont know which one?

PS Can you give me some good tutorial for preg_match function?

Maybe I forgot to say... I dont know how long is image name or what is image name exactly. I need function for extract only what is on right side from last forward slash.

$pattern = '/[\w\-]+\.(jpg|png|gif|jpeg)/';
$subject = 'http://something.com//data/images/2013-10-03/someimage.png';
$result = preg_match($pattern, $subject, $matches);
echo $matches[0]; //someimage.jpg

No need for regex or anything fancy:

$var = "http://something.com/data/images/2013-10-03/someimage.jpg";

$image = basename($var);

U need use preg_replace() and u can try use online for play with regular, it is a fast way to learn regex. http://preg_replace.onlinephpfunctions.com/

For example: /\\/someimage.jpg/ replace on '' (null).

It will return http://something.com//data/images/2013-10-03 from http://something.com//data/images/2013-10-03/someimage.jpg .

You can use Simple HTML DOM Parser to get href between the a tags. For example:

foreach($html->find('a.[class="your class"]') as $var) // echo "href." >sometext
";

hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM