Koopa Posted September 14, 2009 Share Posted September 14, 2009 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 https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/ Share on other sites More sharing options...
Garethp Posted September 14, 2009 Share Posted September 14, 2009 Use this instead '/\]+)"\]/' Link to comment https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918013 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Share Posted September 14, 2009 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 https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918015 Share on other sites More sharing options...
Koopa Posted September 14, 2009 Author Share Posted September 14, 2009 Use this instead '/\]+)"\]/' Sorry, instead of what? Might be clearer (if you dont mind) re-writing the preg_replace() function in full As for your suggest kratsg, thats a wicked way of doing it Link to comment https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918022 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Share Posted September 14, 2009 Use this instead '/\]+)"\]/' Sorry, instead of what? Might be clearer (if you dont mind) re-writing the preg_replace() function in full As for your suggest kratsg, thats a wicked way of doing it 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 https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918031 Share on other sites More sharing options...
Koopa Posted September 14, 2009 Author Share Posted September 14, 2009 I really like that. Thank you Link to comment https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918033 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Share Posted September 14, 2009 This shortcut generally holds when you're looking at [***][/***] tags only... if it's something like which usually doesn't have a closing tag, it won't work... depends on how you like your bbcode. Link to comment https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918040 Share on other sites More sharing options...
Koopa Posted September 15, 2009 Author Share Posted September 15, 2009 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 Link to comment https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-918657 Share on other sites More sharing options...
kratsg Posted September 16, 2009 Share Posted September 16, 2009 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 https://forums.phpfreaks.com/topic/174141-hopefully-a-simple-error-msg-some-bbcode-handling/#findComment-919271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.