开发者

PHP remove page name Regex - preg_replace

开发者 https://www.devze.com 2023-03-05 21:17 出处:网络
I have this url (several similar ones).. images/image1/image1.jpg images/images1/images2/image2.jpg images/images2/images3/images4/image4.jpg

I have this url (several similar ones)..

images/image1/image1.jpg
images/images1/images2/image2.jpg
images/images2/images3/images4/image4.jpg

I have this regex: but I want it to strip away the image name from the string:

<?php $imageurlfolder = $pagename1; 
  $imageurlfolder = preg_replace('/[A-Za-z0-9]+.asp/', '', $pagename1);?>

the string woul开发者_如何学编程d look like the url's above images/images2/images3/images4/ but without the image4.jpg

hope you can help

Thanks


For this particular purpose function dirname() would be sufficient:

<?php echo dirname('images/images2/images3/images4/image4.jpg'); ?>

Would return:

images/images2/images3/images4


I think you can use the dirname function

for instance (from that page)

dirname("/etc/passwd")

would print

/etc


A quite straightforward way to do it:

preg_replace("#(?<=/)[^/]+$#","",$your_string);

It will remove everything between the last / and the end of the string.

Edit: as many peopole pointed out, you can also use dirname which might proof faster…

0

精彩评论

暂无评论...
验证码 换一张
取 消