我有一个数组调用$urls
,我希望删除数组中每个元素的http
之前的所有内容
假设
$urls[1] = hd720\u0026url=http%3A%2F%2Fr2---sn-h50gpup0nuxaxjvh-hg0l.googlevideo.com%2Fvideoplayback%3Fexpire%3D1387559704%26fexp%3D937407%252C908540%252C941239%252C916623%252C909717%252C932295%252C936912%252C936910%252C923305%252C936913%252C907231%252C907240%252C921090%
我希望它是
$urls[1] = http%3A%2F%2Fr2---sn-h50gpup0nuxaxjvh-hg0l.googlevideo.com%2Fvideoplayback%3Fexpire%3D1387559704%26fexp%3D937407%252C908540%252C941239%252C916623%252C909717%252C932295%252C936912%252C936910%252C923305%252C936913%252C907231%252C907240%252C921090%
这里我只给出了$ urls [1]的示例,但我想删除每个字符,直到为数组的所有元素找到http
我试过
$urls = strstr($urls, 'http');
$urls = preg_replace('.*(?=http://)', '', $urls);
两者都不起作用
答案 0 :(得分:3)
将array_map()
与回调函数一起使用:
$urls = array_map(function($url) {
return preg_replace('~.*(?=http://)~', '$1', urldecode($url));
}, $urls);
答案 1 :(得分:0)
$furls = array_map('filterArr',$urls);
function filterArr($v)
{
return urldecode(strstr($v,'http'));
}
print_r($furls);
答案 2 :(得分:0)
我会这样做:
foreach($urls as $key=>$val) {
$e = &$urls[$key]; // notice the & sign
// now whatever you do with $e will go back
// into the original array element
$e = preg_replace(.............);
}
我总是使用这种技术来转换数组,因为它快速而有效。 array_walk / array_filter方式也很好,但是当你的数组从中到大时,它会慢得多。
答案 3 :(得分:0)
你可以在http之前删除所有内容。爆炸。
$string = explode("http", $urls); // Hold the url and cut before the http
$str = $string[0]; // Hold the first cut - E.G : hd720\u0026url=
echo $str; // Hold the first cut - E.G : hd720\u0026url=
另请注意$string[1];
将保持http:`%3A%2F%2Fr2 --- sn-h50 ...
所以你可以这样做:
$str1 = $string[1];
$fixedUrl = 'http'.$str1; // will hold the fixed http : http%3A%2F%2Fr2---sn-h50gpup0nuxaxjvh-hg0l...
答案 4 :(得分:0)
你错过正则表达式的分隔符,preg_replace在数组上运行良好:
$urls = preg_replace('~.*(?=http://)~', '', $urls);
// add delimiters __^ __^
我使用~
来避免转义//
,在这种情况下,它会是:
$urls = preg_replace('/.*(?=http:\/\/)/', '', $urls);
// add delimiters __^ __^