News:

Wondering if this will always be free?  See why free is better.

Main Menu

Using PHP in BBcode

Started by Arganort, August 31, 2013, 07:22:47 AM

Previous topic - Next topic

Arganort

Version:SMF 2.0.5

Hi Guys,
I would like to understand how to use PHP within a BBcode,
As a tester I've tried this in Subs.php

//  [rnd]100[/rnd] generate number between 1-100

         array(
            'tag' => 'rnd',
            'before' => '<?php echo rand(1, ',
            'after' => ');?>',
         ),

But I know I'm doing something wrong, can you put me right ?

Thanks

Arg

Arantor

There's several problems with that.

Firstly, no, it won't work. That would imply that bbcode can be arbitrarily run by users and would be a very bad thing for security.

Secondly, if you do it that way, the number will be regenerated every time the page is viewed, so every person who sees it will see a different number and I'm fairly sure that's not what you want.

Better still, what exactly DO you want to do?

Argonort

Hi Arantor,
My actual task is a little bigger,
I currently use BBcoded Iframes to embed PHP/MySQL tables into opening posts (which collect certain data from the same thread)
but I am constantly messing with sizing & its messy.
so what I actually want to do is use BBcode/PHP to create the tables from scratch.

I just used the random thing as short example so I can grasp how you would run PHP from within BBcode
to save me getting me getting lost in the code.

Thanks
Arg

(NB, I have had to create a new account because changing email has locked me out of the original & Fasthosts seem to be spamming you, Sorry)

Arantor

I kind of suspected as much.

So where is this data being pulled exactly? I'm concerned that it won't actually be available to the bbc parser at the time you're trying to do the parsing...

Argonort

It will be pulled directly from a view on the smf_messages table, so most filtering is already done,
Could I not just open and close connection within the BBCode ?

I've been searching/reading here most of the morning & you have touched on this a couple of times
using validate & function, but I'm a bit out my depth & could do with a simplify.

Thanks again
Arg

Arantor

Um, no, you couldn't exactly do that, partly for the serious performance hit and partly because it'd be an absolute mess.

This is why I wanted to know what you're trying to do in totality so that I could suggest a better way of trying to do it. I'm pretty sure that using bbcode is actually the worst way for what you're trying to implement but since you've been very vague about what you're trying to do, I can't help you.

Argonort

#6
Not intentionally Chap, I'll to try and put some meat on it
LINK

Players post a time usually followed by a bit of trash talk,
the table plucks the times, picks each drivers best then puts them in order.

It works well but I want to move away from iframes but still allow anyone to post a time  trial using BBcode

Didn't meant to be vague, just didn't want to bore anyone :)



Arantor

Hmmm. I think you're going a long way to make it more complex for yourself. Also, when it comes to asking for help, it's always best to provide everything even if you think it's not relevant - because it probably is.

So, let's break this down. You want to get all the posts in the current topic, sift through them for times, and then display a list of the best.

The problem with using bbcode is that, ultimately, I'm not sure there's a bbcode style that actually works. validate is a parameter that defines a function to be called when the bbc is parsed, in theory to validate the contents but it can and has been abused to do something else, namely call dynamic content. The problem is that not all bbc types actually invoke the validate function because they don't necessarily have content to be validated or content that *can* be validated. closed and basic parsed for example (e.g. for the former, [hr] or for the latter [b]) so that's no good to you.

On the flip side, do players actually need to be able to use this arbitrarily? Seems to me that all you're doing is taking a single board and each topic is one track and everyone is competing in that topic about that course.

In other words, I don't think you actually need a bbcode, especially since I don't really think you need this to be as arbitrary as you might first think. Seems to me that the ideal solution would be to check the board you're in, if it's in board 10 or not (your current HLC board) and if so, perform the search and stuff it into the first post if you're currently on the first page of results.

Does that make sense? (This is why I insist on getting details. Very often people are focused on how they think they have to do something, rather than actually solving their problem, which in your case is nothing to do with bbcode)

Argonort

But the point is it need to be dynamic fella,

At the moment,
Bill58 can post [HLC]01/01/01[HLC] with the trial details, go on holiday & leave it to auto update for a fortnight,

I understand some of it is unorthodox, but if you could give me a clue on the opening post random thing,
I might be able to achieve what I want with a bit of spit & duct tape.

Ta
Arg

Arantor

Yay for more requirements that you didn't bother to explain. You see, you didn't explain why it had to be like that. In fact you didn't explain the whole date thing, what does that even mean...? Posts up to that point? Posts after that point?

Sorry, don't have the patience to help you, I made it very clear already that to get the best help you need to provide all the details and you're still adding bits to it that you didn't explain. I just hope that someone else will be able to help you achieve what you want (but then you'll likely be unsatisfied with it anyway because you'll end up with exactly what you asked for, not what would be ideal for your situation)

Good luck.

Argonort

Not quite sure what's just happened ...
I appreciate the help Arantor & understand I need you more than you need me,
I thought I'd give enough of a description, obviously not ...







 

Argonort

That said, I still have the problem ...
If someone could  show me how to incorporate   <?php echo rand(1, number);?>
Into a bbcode so [rnd]50[/rnd] would return a random number up to 50

I would be very grateful,
It will allow me to go away & make my own mistakes

Thanks
Arg

butch2k

Check preparsecode in Subs-Post.php
add something like this at the end before the closing }
$message = preg_replace('~\[rnd\](\d+)\[/rnd\]~ie', "my_random_func('\$1')", $message);

where my_random_func is your random generating function. It should do the trick i believe.


Argonort

#13
Thanks butch, that gives me the gap I need,
now let's see what I can force through it :)

Arg

$message = preg_replace('~\[rnd\](\d+)\[/rnd\]~ie', "rand(1, ('\$1'))", $message);

Arantor

And it won't work properly. Partly because /e is deprecated in PHP 5.5 for performance and security reasons and partly because it's in the preparser, not the main parser. Which means it'll be run at the point a post is *saved* not when a post is *displayed*.

In fact, that's the other problem, shoehorning it into the bbc parser means things like search, RSS feeds and all the other stuff all has to deal with this and it's all a non trivial performance consideration. But I guess that stuff doesn't matter as long as you get to have your bbcode.

The reason I basically left you to it is because suddenly you introduced a parameter to the bbc... the reason I wanted to know about that is because there are 7 types of bbcode and the type of bbcode makes a difference, but I guess that's also irrelevant too...

butch2k

my bad i thought he wanted to generate a random number at post time... he needs to do it through parse_bbc in that case but it will change at every page load.

Of course he should use preg_replace_callback on php 5.5 (like SMF 2.1...) which is fairly easy.

Arantor

No, as he said in the thread, that was just an example...

butch2k

Quote from: Arantor on August 31, 2013, 04:39:21 PM
No, as he said in the thread, that was just an example...
I know about the random being just an exemple, i just did not see the requirement for it to be at display time.

In fact i'm at a loss understanding what he really want to achieve :)
Let me know if i correctly understood this time:
He wants a bbc in the first post of a thread which collects data from the other message in the same thread ?
if so it's a potential performance killer...

Arantor

Nope, he wants bbc to be able to be used in any post in the thread that collects some data from the thread based on the content of the bbc but never explained what that content means in relation to the content of the thread.

butch2k

Ok thanks ! He definitely needs to explain what he wants to achieve.

Advertisement: