我是php的新手。我真的需要你的帮助来编写一个函数来转换下面的URL
来自原始网址:http://www.domain.com/blahblah/**ID**/**FileName**.html
到新网址http://statics.domain.com/download/**ID**/**Filename**.mp4
我想在新网址中获得 ID 和文件名。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
一种肮脏的方式:
$url = "http://www.domain.com/blahblah/ID/FileName.html";
$replace = array("http://www.domain.com/blahblah/", ".html");
$by = array("http://statics.domain.com/download/", ".mp4");
$newurl = str_replace($replace, $by, $url);
它基本上取代了你想要的东西......你想要什么。但仅此而已,我很确定一个更好的答案是可能的技术写作。 ;)
答案 1 :(得分:0)
一种黑客的方式是explode()字符串/
并取最后两个数组元素。最后一个元素是 ID ,其中最后一个元素是文件名。
您需要对文件名执行substr()以从字符串中删除最后.html
个字符。
你就是这样做的。
<?php
$url="http://www.domain.com/blahblah/ID/FileName.html";
$parts=explode("/",$url);
$totalparts=sizeof($parts);
$id=$parts[$totalparts-2];
$filename=substr($parts[$totalparts-1], 0, -5);
答案 2 :(得分:0)
解决。这很简单而且有效。
function converturl($url){
if(preg_match('/.*domain.com\/.*\/(.*?)\/(.*).html/is', $url, $id)){
$newurl = 'http://static.domain.com/download/'.$id[1].'/'.$id[2].'.mp4';
}else{
$newurl = 'URL is not support';
}
return $newurl;
}