Page 1 of 1

[SOLVED] hard to explain! like our formatting bar on phpBB

Posted: Mon Mar 08, 2004 6:19 am
by malcolmboston
ok guys im currently creating a quite advanced CMS with group policies, content creation for sites, user administration etc for a site im working on for a client

now these guys say they have HTML knowledge but ive seen there knowledge and i certainly wouldnt call it that

now as we all know, HTML can throw up various errors when formatted incorrectly within PHP Tags (obviously seperation isnt really an option here) so i need a way for a user to click on a button and automatically write out say the correct say <font color=\"#ff0000\" tag pretty much like this 'site does it' as i dont want some idio breaking the site because he doesnt know how to format the tag correctly

can anyone advise me on how to go about this

i was looking into BBcode and diownloaded a full script for it, but cant really make any sense of it

any help would be much appreciated
Mal

Posted: Mon Mar 08, 2004 7:26 am
by twigletmac
You should just be able to use [php_man]addslashes[/php_man]() so that the user doesn't have to know that certain things need to be escaped - you do it automatically for them. [php_man]stripslashes[/php_man]() then makes sure that when they try and edit their code they don't see backslashes.

Mac

Posted: Mon Mar 08, 2004 7:34 am
by malcolmboston
ok but how can i implement a "['BOLD']" kind of system is that just a simple str_replace because the users of the system may not necessarily know html

Posted: Mon Mar 08, 2004 7:42 am
by twigletmac
You can use regular expressions, for example to convert ... to bold text you could do:

Code: Select all

$formatted_text = preg_replace('/\[b\](.+?)\[\/b\]/is', '<b>\1</b>', $text_with_bbcode);
You can also put a bunch of formatting options into arrays and then pass the arrays (one for the patterns to match and one for what to transform matched data to) to the [php_man]preg_replace[/php_man]() function.

Mac

Posted: Mon Mar 08, 2004 7:49 am
by malcolmboston
twigletmac wrote:You can use regular expressions, for example to convert ... to bold text you could do:

Code: Select all

$formatted_text = preg_replace('/\[b\](.+?)\[\/b\]/is', '<b>\1</b>', $text_with_bbcode);
thanks twiglet

however is there any chance of breaking down what that string means as that just looks like a dozen/\ ive looked at the manual and it says nothing about what it means

Thanks In Advance

Posted: Mon Mar 08, 2004 8:08 am
by twigletmac
Basically the regex string is:

Code: Select all

/\&#1111;b\](.+?)\&#1111;\/b\]/is
broken down this becomes
  • / - the delimiters of the expression
  • \[b\] = match one (the square brackets have to be escaped as they have a special meaning in regex)
  • (.+?) - match any characters (the dot is any character, the plus means any number of the previous) but don't be greedy (the question mark) - stop matching when the next pattern is found. The brackets are used to enclose bits of the matched pattern that you want to replicate in the formatted text (the \1 in <b>\1</b>, if there was another lot of bracketed text this would be \2).
  • \[\/b\] - match one
  • i - be case-insensitive
  • s - include newlines when doing the full character search (i.e. using the dot metacharacter).
For more info on syntax:
http://php.net/manual/en/pcre.pattern.syntax.php
and on the modifiers:
http://php.net/manual/en/pcre.pattern.modifiers.php
Mac

Posted: Mon Mar 08, 2004 8:10 am
by malcolmboston
looks a tad complex but im sure ill work it out

thank you twig, youve been very helpful