I am confuse how to replace string preg replace

n3ISe

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.

Kirbo

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to use preg_replace_callback if i want to use same "string" twice on a callback?

분류에서Dev

preg_replace() returns empty string

분류에서Dev

Preg_match_all and then replace for an input string

분류에서Dev

preg_replace only string, not in a or img tags

분류에서Dev

How to replace strings in a string

분류에서Dev

How do I write function using reg ex through preg_replace()

분류에서Dev

PHP preg_replace

분류에서Dev

Preg Replace 콜백

분류에서Dev

preg_replace only string, not in a or img 태그

분류에서Dev

preg_replace() the same string by increment variable based on occurrences order

분류에서Dev

how can I remove white space in a string and replace it with (;) in batch

분류에서Dev

How do I replace HTML content with a string in python using BeautifulSoup?

분류에서Dev

preg_replace / string_replace 기호-또는 *를 공백으로

분류에서Dev

preg_replace to replace '${abc}' variable

분류에서Dev

how to replace characters in a string with integers?

분류에서Dev

Converting snippet from PHP - how to "convert" preg_replace

분류에서Dev

how to use preg_replace with dimensional pattern and replacement in php?

분류에서Dev

replace `_` with `-` in a string

분류에서Dev

php preg_replace in python

분류에서Dev

PHP preg_replace HREF

분류에서Dev

Preg_replace-개수

분류에서Dev

MySQL Preg Replace 명령

분류에서Dev

preg replace in php with custom tags

분류에서Dev

preg_replace issues with hyperlinks

분류에서Dev

Search and replace hash value. What am I doing wrong?

분류에서Dev

How to sed and replace string with a folder path

분류에서Dev

how to replace number in a string that contains brackets in php

분류에서Dev

how to replace @ string and capitalize letters with sed

분류에서Dev

How to replace a string according to their length with "*" (astrik) letter

Related 관련 기사

뜨겁다태그

보관