I am stuck the whole day thinking how to change [img]imagesrc[/img]
to <img src='imagesrc' />
. Well, in this case i can use str_replace
to solve. Just curious is it possible to do it with preg_replace
?
another problem i cant solve using str_replace
is change [size=14]text[/size]
to something like <font style='font-size:14px'>text</font>
.
Someone please help me.
Edited
$content = [img]imagesrc[/img];
$content = str_replace("[img]", "<img src='", $content);
$content = str_replace("[/img]", "' style='width:100%'/>", $content);
This is what I had done with [img] using str_replace
. Just want to know any solution by using preg_replace
since I cant solved [size=14]text[/size]
with str_replace
.
I just wonder why people always complaint my question is not related or any other reason. I am a newbie thats why I seeking for help here. Isn't here a platform seeking for help? I am not asking for answer directly without trying, I just asking for alternative and better solution to improve my skill.
Here's an example:
<?php
$content = '[img]imagesrc[/img][size=10]Text with size of 10[/size]';
$html = preg_replace("#\[img\](.+)\[\/img\]#iUs", '<img src="$1"/>', $content);
$html = preg_replace('#\[size\=(\d+)\](.+)\[\/size\]#iUs', '<font style="font-size: $1px;">$2</font>', $html);
print $html;
?>
You could also make it a function, like:
function parseBBcode($content) {
$html = $content;
$html = preg_replace('#\[img\](.+)\[\/img\]#iUs', '<img src="$1" alt="Image" />', $html);
$html = preg_replace('#\[size\=(\d+)\](.+)\[\/size\]#iUs', '<font style="font-size: $1px;">$2</font>', $html);
$html = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $html);
$html = preg_replace('#\[link(?|=[\'"]?+([^]"\']++)[\'"]?+\]([^[]++)|](([^[]++)))\[/link\]#iUs', '<a href="$1">$2</a>', $html);
return $html;
}
And then use it like:
print parseBBcode('[img]imagesrc[/img] [img]imagesrc2[/img] [img]imagesrc3[/img] [size=10]Text with size of 10[/size] [b]text to be bolded[/b] [link=http://www.google.com]Link to google[/link] [link]http://www.google.com[/link');
or:
$content = '[img]imagesrc[/img] [img]imagesrc2[/img] [img]imagesrc3[/img] [size=10]Text with size of 10[/size] [b]text to be bolded[/b] [link=http://www.google.com]Link to google[/link] [link]http://www.google.com[/link]';
print parseBBcode($content);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다