Jump to content

Recommended Posts

As the title say's. This bit of code is giving some error.

 

<?php

class bbcode_engine	{
function bbcode_parser($code)	{
	$string = '';
	$string .= preg_replace('[b]/(\.*?/)\[/b]','<strong>$1</strong>',$code);
	return $string;
}
}

$bbcode = new bbcode_engine();

echo $bbcode->bbcode_parser('[b]Testing[/b]');

?>

 

This is the error i am getting

 

Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in /home/deadlyki/public_html/test.php on line 6

Link to comment
https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/
Share on other sites

I have figured a solution.

 

	$search = array
	(
		'/(\[[bb]\])(.+)(\[\/[bb]\])/',
		'/(\[[ii]\])(.+)(\[\/[ii]\])/',
		'~\[size=(.+)\](.+)\[\/size\]~is',
		'/\n/'
	);
	$replace = array
	(
		'<strong>\\2</strong>',
		'<em>\\2</em>',
		'<span style="font-size: \\1;">\\2</span>',
		'<br />',
	);

I had figured it out. But now i have ran into another problem.

 

My code is:

 

<?php

class bbcode_engine	{
function bbcode_parser($code)	{
	$search = array
	(
		'~\[b\](.+)\[\/b\]~is',
		'~\[i\](.+)\[\/i\]~is',
		'~\[size=(.+)\](.+)\[\/size\]~is',
		'/\n/',
		'~\
[center](.+)\[\/center\]~is',
		'~\[left](.+)\[\/left\]~is',
		'~\[right](.+)\[\/right\]~is',
	);
	$replace = array
	(
		'<strong>\\1</strong>',
		'<em>\\1</em>',
		'<span style="font-size: \\1;">\\2</span>',
		'<br />',
		'<div align="center" style="padding: 0;">\\1</div>',
		'<div align="left" style="padding: 0;">\\1</div>',
		'<div align="right" style="padding: 0;">\\1</div>'
	);
	return preg_replace($search, $replace, $code);
}
}

$bbcode = new bbcode_engine();

echo $bbcode->bbcode_parser('We try [b]bold[/b]
Then we try [i]italic[/i]
now we try size [size=24]24[/size]
[center][b]Center[/b][/center]


[left]Left[/left]


[right]Right[/right]
');

?>

 

But it output's it like this:

 

We try bold[/ b]

Then we try italic

now we try size 24

                                          [ b]Center

 

Left

 

                                                                                              Right

 

You can see it at http://www.deadlykillah.com/test.php. Any fix for this?

it's because your (.+) is greedy.  You have 2 bold tags in your string so the (.+) in between your first b tags is matching all the way to the last closing b tag.  Make it non-greedy by doing this (.+?) for all of them.

 

But also, you're going to run into problems later on if someone does this:

 

[b][/b]

 

with any of your tags, not just the b tags.  I'm pointing out to you the lack of stuff between them.  Your patterns will cause problems because the + expects at least one character.  So use * instead of +.  * is 0 or more chars.

 

So overall, change all your (.+) to (.*?)

The posix regex engine and syntax (what ereg_xxx uses) is generally thought to be simpler to use, but not offer you as much control over what gets matched and how.  The pcre engine and syntax (what preg_xxx uses) is generally thought to have a higher learning curve, but the return on that is being able to write more complex patterns.  Also, pcre is perl compatible (hence the engine name: Perl Compatible Regex Engine), meaning more pattern portability.  Overall they mostly have the same syntax.  There are some differences here and there, most of them are how things are matched though.

 

Most of the time people generally don't care or need something complicated or portable enough to warrant having to pick one over the other.  But what might be slightly more relevant for average joe user is that the posix functions (ereg_xxx) will no longer be part of php's core package as of php6, meaning, if you write your scripts using ereg_xxx functions and plan on having them around a long time, you're going to have to install a separate package when (if) you upgrade your php. 

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.