Welcome Guest, Not a member yet? Register   Sign In
Why doesn't my parser work?
#1

[eluser]Zero-10[/eluser]
So here's a very basic parser to find and replace bbcode. So far it should only support bold but it doesn't.

Idea: I am fetching a blog post from the database and in the database the text looks like:
Code:
[b]This is a bold test[/b].

It should be displayed bold in the view. I know I could use someone's parser but I want the theory down because I don't want to rely on everyone but instead be able to give advice in the future :D

[b]Controller:[/b]
[code]blah blah blah...
//Load Page specific Helpers

$this->load->helper('bbcode');

// Now we want to load the php modules
$this->load->view('overall-header', $data);

Helper: (helpers/bbcode_helper.php)
Code:
function bbcode($input){

$input = strip_tags($input);
$input = htmlentities($input);

$search = array(
  '/\[b\](.*?)\[\/b\]/is'
);

$replace = array(
  '<strong>$1</strong>'
);

return preg_replace($search,$replace,$input);

}

Thank you for your time!
#2

[eluser]PhilTem[/eluser]
Hm... If I just take your code snippets it works for me (not wrapped in CI, though).

What I was wondering:
a) what is the output you get
b) Do you do anything about calling the bbcode()-function?
c) What is inside $data?
#3

[eluser]Aken[/eluser]
Yeah your bbcode() function works fine - just messing up using it somewhere along the line.
#4

[eluser]Zero-10[/eluser]
I noticed that I don't have

Code:
if (!defined('BASEPATH')) exit('No direct script access allowed');

in my helper so I'll try that at home and do some more debugging and let you guys know on the progress. Smile
#5

[eluser]srpurdy[/eluser]
Are you actually wrapping your string in your view with the bbcode function. Seems like there has to be a reason why it's not parsing. I know it sounds like a stupid thing, but sometimes that happens to everyone lol. Smile

Like in your view file
Code:
&lt;?php $mystring = '[b] my bold text[/b]';?&gt;
&lt;?php echo bbcode($mystring);?&gt;

(likely you have this from a db field but that's the example usage to what you created)
#6

[eluser]Zero-10[/eluser]
[quote author="srpurdy" date="1344635956"]Are you actually wrapping your string in your view with the bbcode function. Seems like there has to be a reason why it's not parsing. I know it sounds like a stupid thing, but sometimes that happens to everyone lol. Smile

Like in your view file
Code:
&lt;?php $mystring = '[b] my bold text[/b]';?&gt;
&lt;?php echo bbcode($mystring);?&gt;

(likely you have this from a db field but that's the example usage to what you created)
[/quote]

Call me stupid because I forgot to upload my view file!!! LOOOOOOL Stupid is what stupid does. I really should reconsider my field of study if I keep doing these stupid mistakes. I may be sort of a noob at this but that doesn't mean I should keep doing the same stupid mistakes over and over again. Much thanks to that advice!




Theme © iAndrew 2016 - Forum software by © MyBB