preg_replace怎么将src="/n1/images替换成src="images
src="/n1/images 中的n1 可能是其他字符,比如说m2或者py等。
不懂正则替换,请指点。
$s =<<< TXT<img src="/n1/images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/x2/images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;echo preg_replace('#(?<=src=").*/([^"]+)#', '$1', $s);
<img src="images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >
$s =<<< TXT<img src="/n1/images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/x2/images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;echo preg_replace('#(?<=src=").*/([^"]+)#', '$1', $s);
<img src="images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >
替换成 类似的应该怎么做,
你的需求不明确,前后矛盾,到底保留文件本身,还是保留文件名以及他的父级目录。
你的需求不明确,前后矛盾,到底保留文件本身,还是保留文件名以及他的父级目录。
$str="src='/n1sss/images'“;
$zz="/src='\/.*\/(.*?)'/";
echo preg_replace($zz,'$1',$str);
$str="src='/n1sss/images'“;
$zz="/src='\/.*\/(.*?)'/";
echo preg_replace($zz,'$1',$str);
<?php$s =<<< TXT<img src="/a/b/images/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$pattern="/\/.*\/(.*?)\/(.*?)/";echo preg_replace($pattern, '$1/$2', $s);?>
<?php$s =<<< TXT<img src="/a/b/images/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$pattern="/\/.*\/(.*?)\/(.*?)/";echo preg_replace($pattern, '$1/$2', $s);?>
<?php$s =<<< TXT<img src="/a/b/images/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$pattern="/\/.*\/(.*?)\/(.*?)/";echo preg_replace($pattern, '$1/$2', $s);?>
$s =<<< TXT<img src="/n1/images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/x2/images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$n = 0; //只留文件名echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 1; //保留一级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 2; //保留二级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);
<img src="images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >
$s =<<< TXT<img src="/n1/images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/x2/images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$n = 0; //只留文件名echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 1; //保留一级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 2; //保留二级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);
<img src="images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >
$s =<<< TXT<img src="/n1/images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/x2/images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$n = 0; //只留文件名echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 1; //保留一级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 2; //保留二级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);
<img src="images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >
$s =<<< TXT<img src="/n1/images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/x2/images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="/a/b/images2/hehehahha" alt="preg_replace怎么将src="/n1/images替换成src="images" >TXT;$n = 0; //只留文件名echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 1; //保留一级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);$n = 2; //保留二级目录echo preg_replace("#(?<=src=['\"]).*/((?:.*/){{$n}}[^'\"]+)#", '$1', $s);
<img src="images" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images1" alt="preg_replace怎么将src="/n1/images替换成src="images" ><img src="images2" alt="preg_replace怎么将src="/n1/images替换成src="images" >
$str="src='/n1sss/images'“;
$zz="/src='\/.*\/(.*?)'/";
echo preg_replace($zz,'$1',$str);