Jump to content

Recommended Posts

Hello there,

 

I am making a little script and i need your help because im not very good at regex and these sorts of things.

 

I have the following code:

function process_bbcode($text)
{
// ITALICS, BOLD, LINK, UNDERLINE
$string = $text;
$bb_replace = array ('/(\[[bb]\])(.+)(\[\/[bb]\])/','/(\[[ii]\])(.+)(\[\/[ii]\])/','/(\[[uu]\])(.+)(\[\/[uu]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/');
$bb_replacements = array ('<b>\\2</b>','<i>\\2</i>','<u>\\2</u>','<a href="\\2">Link</a>');
//$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>');
$string = preg_replace($bb_replace, $bb_replacements, $string);
return $string;
}

echo process_bbcode("This is [u][i][b]cool[/b][/i][/u] - [url=http://www.google.com].[/url] [img=4567436]");

 

This script works perfect for underlining, bold, italics and links. The problem is that i want to be able to pass the code that the user enters between IMG tags to another function.

 

So when a user types:

[img=4567436]

 

I want my script to extract "4567436" and send it off to another function:

get_image("4567436");

 

Is there anyway that i can do this?

Link to comment
https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/
Share on other sites

The problem is that i want to be able to pass the code that the user enters between IMG tags to another function.

 

One example to your solution could be:

 

function imageText($text){
    echo 'I am ' . $text . ', the text between the bbc [img]http://tags.' . "<br />\n";
}

function process_bbcode($text)
{
// ITALICS, BOLD, LINK, UNDERLINE
$string = $text;
$bb_replace = array ('#\[b\](.+?)\[/b\]#i','#\[i\](.+?)\[/i\]#i','#\[u\](.+?)\[/u\]#i','#\[url=http://([^]]+)\].+?\[/url\]#si');
$bb_replacements = array ('<b>$1</b>','<i>$1</i>','<u>$1</u>','<a href="$1">Link</a>');
$string = preg_replace($bb_replace, $bb_replacements, $string);

if( preg_match('#\[img\]([^]]+)\[/img\]#i', $string, $match) ){
    imageText($match[1]);
}

return $string;
}

echo process_bbcode("This is [u][i][b]cool[/b][/i][/u] - [url=http://www.google.com]google[/url] [img=4567436]");

 

Output:

I am 4567436, the text between the bbc [img] tags.
This is cool - Link [img=4567436]

 

Note.. in your initial regex, you had captures that weren't necessary, so I made some changes there.... I also added the i and s modifiers.. i is case insensitive,  and s if for the dot match all to include newlines (if the situation calls for it).

Link to comment
https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763498
Share on other sites

Thanks a lot but, if there are two image tags then it only parses one of them.

 

function imageText($text){
    echo 'PIC:'.$text;
}

function process_bbcode($text)
{
$string = $text;
//$bb_replace = array ('#\[b\](.+?)\[/b\]#i','#\[i\](.+?)\[/i\]#i','#\[u\](.+?)\[/u\]#i','#\[url=http://([^]]+)\].+?\[/url\]#si');
$bb_replace = array ('#\[b\](.+?)\[/b\]#i','#\[i\](.+?)\[/i\]#i','#\[u\](.+?)\[/u\]#i','#\[url=([^]]+)\].+?\[/url\]#si');
$bb_replacements = array ('<b>$1</b>','<i>$1</i>','<u>$1</u>','<a href="$1">Link</a>');
$string = preg_replace($bb_replace, $bb_replacements, $string);

if( preg_match('#\[img\]([^]]+)\[/img\]#i', $string, $match) ){
    imageText($match[1]);
}

return $string;
}

echo process_bbcode("This is [u][i][b]cool[/b][/i][/u] - [url=http://www.google.com]google[/url] [img=4567436] [img=4567436897]");

 

Output:

PIC:4567436This is cool - Link [img=4567436] [img=4567436897]

Link to comment
https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763553
Share on other sites

Thanks a lot but, if there are two image tags then it only parses one of them.

 

change the if statement to:

if( preg_match_all('#\[img\]([^]]+)\[/img\]#i', $string, $match) ){
    imageText($match[1]);
}

 

and change the function imageText to:

function imageText($text){
    foreach($text as $val){
        echo 'PIC:'.$val;
    }
}

Link to comment
https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763560
Share on other sites

I would suggest having a look at other posts dealing with bbcode and regex (remember, forum search is your friend). If you are also interested in learning regex, you can have a look at our resources page, as well as this regex tutorial.

 

EDIT - Oops.. that first link doesn't seem to work.. basically, type in 'regex bbcode' in the forum search field at the top (without the single quotes.. just a space between both words). This will give you additional info on parsing bbcode.

Link to comment
https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763573
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.