>  기사  >  백엔드 개발  >  求php正则表达式方法

求php正则表达式方法

WBOY
WBOY원래의
2016-06-06 20:45:301241검색

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

中的[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>

中的[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>

不太熟悉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>

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.