Jump to content

[SOLVED] removing a bbcode id number


HaLo2FrEeEk

Recommended Posts

Ok, phpbb, at least, puts a unique ID number into each bbcode tag a user posts, for example, if I were to post bold text and look at the raw code in the database, it would look like this:

 

[b:9f0e836e4c]bold text[/b:9f0e836e4c]

 

Now, it's simple enough to remove those numbers using this regex:

 

[b:[^\]]+]

 

What's really got me stumped, though, is how to do this when the bbcode has attributes, such as size and a quote:

 

Test quote

 

Would end up being:

 

Test quote

 

And:

 

Test size

 

turns into:

 

Test size

 

What I need to know is how I can use one regex to handle both the opening, AND the closing tags for these, and still keeping the attribute value (so saving it to a backreference).  I used this one for size, but it only did the opening tag, and not the closing one:

 

preg_replace("[size(=[0-9]{1,2}):[^\]]+]", "size$1", $text);

 

The thought was that if the attribute was there (i.e., an opening tag) then it would save the backreference and replace it, if it's not there, it will ignore it.  It doesn't work though, and it only replaces the opening tag, forcing me to have another regex to handle the closing tags.

 

What can I do to fix this?

 

Thank you.

Link to comment
Share on other sites

Ok, I got the size one, I feel dumb, all I needed was to add a question mark, to make the part getting the attribute optional, but it won't work for the quote tag, no matter what I try, I can't get this to work.  This regex:

 

]+(=[.]*)]

 

Turns:

 

Test[/quote:bcb8669df7]

 

Into:

 

[quote="HaLo2FrEeEk"]Test[/quote:bcb8669df7]

 

So it works halfway.  I tried adding a question mark, and this regex:

 

]+(=[.]*)?]

 

Turns:

 

Test[/quote:bcb8669df7]

 

Into:

 

[quote]Test[/quote]

 

No attributes.  I'm about to pull my hairs out...and I need them, but this is driving me insane.

Link to comment
Share on other sites

Do you need nesting capabilities?

 

<pre>
<?php
$tests = array(
	'[b:9f0e836e4c]bold text[/b:9f0e836e4c]',
	'[quote:9f0e836e4c="HaLo2FrEeEk"]Test quote[/quote]',
	'[size=14:9f0e836e4c]Test size[/size]'
);
foreach ($tests as $test) {
	echo $test, ' => ';
	echo preg_replace('#
		\[(b|quote|size=\d+):[^]=]+(=".+?")?\]
		(.*?)
		\[/([^:]+)(?:[^]]+)?\]
	#x', '[\1\2]\3[/\4]', $test);
	echo '<br>';
}
?>
</pre>

Link to comment
Share on other sites

Holy!...

 

How in bloody hell did you do that?  Thank you, could you possibly tell me how it works, so I can try to do it myself in the future.

 

Also, could you tell me if I have the below regex right to turn this:

 

[url=http://claninfectionist.com/]Clan Infectionist[/url]

 

into this:

 

<a href="http://claninfectionist.com/">Clan Infectionist</a>

 

REGEX:

 

\[url=(.+?)\](.+?)\[\/url\]

 

Will that work?  If so, is there a better way, if not, what can I use?

 

And thank you again.

 

EDIT:

 

I'd also like to know how I can turn:

 

[url]http://claninfectionist.com[/url]

 

into:

 

<a href="http://claninfectionist.com">http://claninfectionist.com</a>

 

If it's not too much trouble.

Link to comment
Share on other sites

### Inside square braces find...
\[
(b|quote|size=\d+) ### Either one of these.
: ### A colon.
[^]=]+ ### Anything that's not an end square brace or an equals sign.
(=".+?")? ### An optional equals sign follow by quoted text.
\]
(.*?) ### Optional content between the begin and end tags.
### Inside square braces find...
\[
/ ### A forward slash.
([^:]+)(?:[^]]+)? ### Optional non-colon content, followed by a colon and non-end-square-brace content.
\]

 

Based on this, show me what you've tried for the other ones you've posted.

Link to comment
Share on other sites

Ok, lemme try.

 

preg_replace("#\[url((=.+?)?|\])(.+?)[\/url]#", "<a href=\"$1\">$2</a>", $string);

 

I don't know...something seems wrong with that.

 

EDIT: This is the one I tried, it needs to be able to handle both

[url=URL]Something[/url]

and

[url]URL[/url]

:

 

preg_replace("|\[url[=]?(.+?)?\](.+?)\[\/url\]|", "<a href=\"$1\">$2</a>", $string);

 

It doesn't work for the second one

[url]URL[/url]

it only works for the first one.

Link to comment
Share on other sites

<pre>
<?php
$tests = array(
	'[url=http://claninfectionist.com/]Clan Infectionist[/url]',
	'[url]http://claninfectionist.com[/url]',
);
foreach ($tests as $test) {
	echo $test, ' => ';
	echo htmlspecialchars(
		preg_replace("#
			\[url
				(?:=([^]]+))?
			\]
			(.+?)
			\[/url\]
		#xe",
		'"<a href=\"" . ("\1" == "" ? "\2" : "\1") . "\">\2</a>"', $test)
	);
	echo '<br>';
}
?>
</pre>

Link to comment
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.