I want to change [img]src[/img]
to <img src="src" alt="src" >
with regular expression.
I found some examples which convert <img src="">
to [img][/img]
but not my case.
Thank you!
$ret = preg_replace('#\[img\](.+)\[\/img\]#iUs', '<img src="$1" alt="img">', $ret);
But in general you want something like a dedicated phpBB script, or phpBB class. Even PHP itself got BBCode text processor: http://www.php.net/manual/en/book.bbcode.php
你可以测试一下
$str= preg_replace('~\[img\](.*)\[\/img\]~si', '<img src="$1" alt="$1">', $str);
I beleive this article will help you with your problem...
http://thesinkfiles.hubpages.com/hub/Regex-for-BBCode-in-PHP
function parseCode($txt)
{
// these functions will clean the code first
$ret = strip_tags($txt);
// code replacements
$ret = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $ret);
$ret = preg_replace('#\[link\=(.+)\](.+)\[\/link\]#iUs', '<a href="$1">$2</a>', $ret);
$ret = preg_replace('#\[img\](.+)\[\/img\]#iUs', '<img src="$1" alt="Image" />', $ret);
$ret = preg_replace('#\[quote\=(.+)\](.+)\[\/quote]#iUs', '<div class="quote">$2</div><div class="quote-by">By: $1</div>', $ret);
// return parsed string
return $ret;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.