求php正则表达式方法

WBOY
Release: 2016-06-06 20:45:30
Original
1243 people have browsed it

<code>[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg
</code>
Copy after login
Copy after login

中的[img src=xxxx.jpg]改成<img src="./upload/xxxx.jpg" style="max-width:90%" height="100px" alt="求php正则表达式方法" >

回复内容:

<code>[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg
</code>
Copy after login
Copy after login

中的[img src=xxxx.jpg]改成<img src="./upload/xxxx.jpg" style="max-width:90%" height="100px" alt="求php正则表达式方法" >

<code class="lang-php">$str = '[img src=sorqin_2014523174838.jpg] 2013 asdfdfdghhhhhhh[img src=sorqin_2014523174838.jpg]sdgfdgdfghdhgfhjfjghjg';
$str = preg_replace('/\[img\s+src=(.*?)\]/is', '<img  src="./upload/%241"    style="max-width:90%" height="100px" alt="求php正则表达式方法" >', $str);
echo $str;
</code>
Copy after login

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

<code>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"');
</code>
Copy after login

可以把上面的代码贴到浏览器的控制台里试一下
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);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!