首页 > 后端开发 > php教程 > preg_replace怎么将src="/n1/images替换成src="images

preg_replace怎么将src="/n1/images替换成src="images

WBOY
发布: 2016-06-23 13:31:40
原创
862 人浏览过

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" >
登录后复制
登录后复制



谢谢版主,但如果是 preg_replace怎么将src= 怎么办?

preg_replace怎么将src= 替换成 preg_replace怎么将src= 类似的应该怎么做,

你的需求不明确,前后矛盾,到底保留文件本身,还是保留文件名以及他的父级目录。

你的需求不明确,前后矛盾,到底保留文件本身,还是保留文件名以及他的父级目录。



其实就是将 preg_replace怎么将src= 替换成 preg_replace怎么将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);



这个 $zz="/src='\/.*\/(.*?)'/"; 在PHP里面就有错误

<?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);?>
登录后复制
登录后复制
登录后复制



谢谢版主的回答,可能我的问题说的不是很明确,
利用preg_replace怎么将images前面的目录删除掉
preg_replace怎么将src= 或者preg_replace怎么将src=

<?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" >preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=
登录后复制
登录后复制
登录后复制
登录后复制

$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" >preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=
登录后复制
登录后复制
登录后复制
登录后复制



$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" >preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=
登录后复制
登录后复制
登录后复制
登录后复制



$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" >preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=preg_replace怎么将src=
登录后复制
登录后复制
登录后复制
登录后复制



谢谢版主的回答,我有一个文件夹images,我就想保留images文件夹以及后面的目录,images后面的目录有几级是未知的。还请版主帮个忙

$str="src='/n1sss/images'“;

$zz="/src='\/.*\/(.*?)'/";

echo preg_replace($zz,'$1',$str);




我都运行过的没问题,才发出来的啊
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板