简体   繁体   中英

preg_replace a link

I'm wanting to use a preg_replace only to replace

"http://domain.com/dateiIe6EHOnzyl/_DSC7290.jpg.htm"

to

"http://domain.com/dateiIe6EHOnzyl.htm"

p/s: I need using only preg_replace

$link = preg_replace('~/[^/]*(\.html?)$~', '$1', $link);

http://codepad.org/tVtZBD7L

Try without preg_replace(), but with explode() and str_replace(), like in this example:

$str = 'http://domain.com/dateiIe6EHOnzyl/_DSC7290.jpg.htm';
$lastpart = end(explode('/', $str));
$str2 = str_replace('/'.$lastpart, '.htm', $str);
echo $str2;         // http://domain.com/dateiIe6EHOnzyl.htm
$str = 'http://domain.com/dateiIe6EHOnzyl/_DSC7290.jpg.htm';
$str = preg_replace('/(.+)\/.+\.(.+)/','$1.$2',$str);
print $str;

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