Simple clear advice in plain English

Hands on: How to roll your own CMS

How to update your website using a simple content management system

In and out
One of the problems with adding information via a CMS is how exactly do you add it? Do you just allow people to enter text, or do you make provisions for headlines and so on?

Some systems allow you to simply add a headline, and then body text. If you want a subheading, you have to add an extra element, then add another element of body text, and so on, which can be fiddly for a novice.

But then, a block of plain text is awkward too. And what if the original is already formatted, for example, in Microsoft Word?

You could allow users to save pages as HTML from Word, and allow a simple way to upload them.

But what if they have used odd fonts? You could end up with some parts of the site looking truly horrible.

So, the solution I’ve adopted – and remember it might not be suitable for everyone – is to simply allow text to be cut and pasted, and then to use codes to detect when line breaks or headings should appear.

You could, of course, try to detect line breaks with a script, but you then run the risk of them not being consistently used in the original document.

While this is a little more than a straight cut and paste, it’s not too much work.

What I’ve ended up with is something quite simple, where I can enter suitably formatted text into a database field, directly or via a web form.

It’s fairly simple then to change a document, skimming through it and adding the necessary codes.

You could even do a search and replace in Word for the end of paragraph symbol, for instance. But how do you get it onto the web page?

Selecting it from the database is pretty straightforward. Then you need to turn those codes back into something that’s usable for the browser.

To do that, I have a small PHP library included in the pages that need to display database content.

There’s a formatPage function that takes two parameters: the first is the string retrieved from the database, and the second says where on the page it’s going – whether it’s for the right-hand panel, or the main body – so that the correct CSS style names can be used.

In a typical Dreamweaver-style query-based page, all you have to do is say something like:
echo formatPage($row_Content[‘pageText’],’body’) ;

Later in this article, under 'The formatPage function', you will see the code for the formatting function.

As you can see, it’s a simple use of Perl-compatible regular expressions, and you could do the same in your own favourite scripting language.

It’s a touch simplistic too; for example, it doesn’t allow for punctuation in headings, but for many sites something such as this could be a quick solution to making it easy to keep pages up to date.

THE FORMATPAGE FUNCTION
function formatPage( $pageInfo, $where ) {
// format the text with appropriate CSS and HTML tags
// we use @@ to indicate end of para
// links are formatted with WWW:site title,URL:www.whatever
// and headings with %%heading%%
// code is added by using *+ to begin a block
// and *- to end it, with ** as a

if ( $where == ‘panel’ ) {
$para = ‘rhText’ ;
$link = ‘rhLink’ ;
$head = ‘’ ;
$code = ‘’ ;
} else {
$para = ‘body’ ;
$link = ‘bodyLink’ ;
$head = ‘bodyHead’ ;
$code = ‘bodyCode’ ;
}
$pageInfo = ‘

’ . $pageInfo . ‘< /p >’ ;
$pageInfo = preg_replace(‘/\@\@/’,’

’, $pageInfo ) ;
$pageInfo = preg_replace(‘/WWW:([\w\s]+),URL:([^\s]+)\s/’, ‘$1 ‘, $pageInfo ) ;
$pageInfo = preg_replace(‘/\%\%([\w\s,]+)\%\%/’, ‘

$1

’, $pageInfo ) ;
$pageInfo = preg_replace(‘/\*\*/’, ‘
’, $pageInfo ) ;

$pageInfo = preg_replace (‘/\*\+/’,’

’, $pageInfo ) ;
$pageInfo = preg_replace(‘/\*\-/’,’

’, $pageInfo ) ;
return $pageInfo ;
}

Formatting text
Here are some formattiing examples:
1. %%This is a heading%%
2. Each paragraph is terminated by two at symbols, like this.@@
3. If a new line is all that’s needed, then two asterisks can be used instead, and here’s how we also add a link:**
4. WWW:Personal Computer World,URL:www.pcw.co.uk will be detected and turned into a link

For more articles on web development, click the tag below.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Google illustration

Make the most of a Google search

A Google search can be easy, but did you know that you can refine that search? A little thought can improve the chances of finding exactly what you're after

Restore deleted files illustration

Retrieve accidentally deleted files

No matter how careful you are, things can get thrown away by mistake. We show you a free beta tool called Osforensics that may be able to help get them back

Word's undo feature

How to fix common PC errors

It’s easy to hit the wrong key when using a PC and throw yourself into a panic. There's no need to worry - we have easy fixes for 30 common everyday errors

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive