Heim  >  Artikel  >  Backend-Entwicklung  >  php的str_replace() 函数使用正则

php的str_replace() 函数使用正则

WBOY
WBOYOriginal
2016-06-23 14:23:441358Durchsuche

字符串如下:
“http:\/\/www.sina.com\/music1\/23412455.mp3?time=2234523&type=mp3”
“http:\/\/www.sina.com\/music2\/4325243.mp3?time=2234523&type=mp3”
“http:\/\/www.sina.com\/music3\/346553.mp3?time=2234523&type=mp3”
需要对http开始,?结束之间的字符串换成“abc”,在php的str_replace函数里,该如何写?谢谢


回复讨论(解决方案)

str_replace 用不了正则,用preg_replace 吧

正则能做到.

$str="http:\/\/www.sina.com\/music1\/23412455.mp3?time=2234523&type=mp3";
str_replace(substr($str,4,strpos($str,'?')),'abc',$str);

$urls = array("http:\/\/www.sina.com\/music1\/23412455.mp3?time=2234523&type=mp3","http:\/\/www.sina.com\/music2\/4325243.mp3?time=2234523&type=mp3","http:\/\/www.sina.com\/music3\/346553.mp3?time=2234523&type=mp3");foreach($urls as $url){	echo preg_replace('@http(.*?)\?@i','abc',$url).'<br>';}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn