Jump to content

(hopefully) a simple error msg + some bbcode handling


Koopa

Recommended Posts

Can anyone tell me why this errors:

 

$output = "[url='test.php']website[/url]";
$output = preg_replace('\[url="([[:graph:]]+)"\]', '<a href="\\1">', $output);

 

with Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\test\outputlib.php on line 2

 

I've spend hours on this and this is how far i've got.  I know its to do with the backslashes but I dont understand Regular expressions well enough.  Thats it, my hours were extremely unproductive :(.

 

---

 

As a BIG side note, what I want to do is have a user input form so that privileged users can input records onto my database. These then get printed on the website. So its quite sensitive.  I would make it all plain text but I want them to be able to create links, bold, italic etc (bbcode). Just a question, if the user inputs "[­b]something" and forgets the closing tag, whats to stop a script from doing the same when it sends the html to the browser? Obviously its down to the script yes? My question is i support, whats a common standard or method to handle this?

Link to comment
Share on other sites

For determining incorrectly matched bbcodes (a quick easy way):

 

$open_bold = substr_count($input,'[b]');
$close_bold = substr_count($input,'[/b]');

if($diff = $open_bold - $close_bold > 0){//more open then closed
for($i=0;$i<$diff;$i++){
$input .= "[/b]";
}
}

 

As a quick example. Should work >.<

Link to comment
Share on other sites

 

Try a more flexible approach:

 

$tags = array('[b]','[i]','[u]','[img]','[url]');
$insertions = array();

foreach($tags as $tag){
$closing_tag = str_replace('[','[/',$tag);//change [b] to [/b]
$opened = substr_count($input,$tag);
$closed = substr_count($input,$closing_tag);

if($diff = $opened - $closed > 0){//more open then closed
	for($i=0;$i<$diff;$i++){
		$insertions[$tag] .= $closing_tag;
	}
}
}

//$insertions will contain an array of the tags that have been opened too many times (as the key) as well as the value for inputting the closed tags...

 

IE:

 

array('[b]'=>'[/b][/b][/b]')

 

If it was opened 3 times but never closed, or something.

Link to comment
Share on other sites

Does anyone have an answer to my initial error?  There was one reply but I didn't understand it.

 

(I'm going away for a week so apologies if i dont get to reply)

 

Thank you in advance

 

All POSIX (IE: "preg_...." functions) pretty much use a pattern for matching purposes. The pattern is generally a string which defines how or what to match in an input string.

 

This means you'll see codes employing some structure:

 

$matches = array();
$pattern = '#some pattern rules#';
preg_match($pattern,$input,$matches);

 

In the above case, '#....#', the pound signs are what we call delimiters. These are what you wrap your pattern in.. and in fact, I don't believe you even need the quotations:

 

$pattern = #some pattern rules#;

 

...for some delimiters (again, not sure, someone more experienced with Regex can guide you on that). However, I can tell you that as far as delimiters go, quotations don't work :-o Two of the most common that I've seen are the pound sign (#) and the forward slash (/). It also helps to note that for escaping characters (say you want to match an actual period(.)). In Regex, a "." means "match any character" so, this doesn't work. We need to do "\." to escape the period and change it from "match any character" to "match a period".

 

This should help you a bit :-) Here's a list of some common characters you'll have to escape to use them as their actual characters:

.
^
*
( and )
[ and ]
{ and }
+,?

 

Side note: escaping backslash

A backslash cannot be escaped by simply doing "\\" like any of the other characters. You'll have to do "\\\" to "match one backslash". The reasoning is something crazy, but just remember the triple :-)

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.