欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

求php正则表达式方法

程序员文章站 2022-06-02 23:36:45
...
[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg

中的[img src=xxxx.jpg]改成求php正则表达式方法

回复内容:

[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg

中的[img src=xxxx.jpg]改成求php正则表达式方法

$str = '[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg';
$str = preg_replace('/\[img\s+src=(.*?)\]/is', '求php正则表达式方法', $str);
echo $str;

不太熟悉PHP的正则,如果是在js里的话,可以这样

var str = '[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg';
str.replace(/\[/g, '').replace(/(src=)(.+?\.jpg)/g, '$1"./upload/$2" width="100px" height="100px"');

可以把上面的代码贴到浏览器的控制台里试一下
PHP中的话,使用PHP里相应的replace方法应该就可以了

$str = '[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg'; $hack = array('/\[/', '/\]/', '/(src=)(.+?\.jpg)/'); $replace = array('', '$1"./upload/$2" width="100px" height="100px"'); preg_replace($hack, $replace, $str);

相关标签: php 正则表达式