<code> tags

This is a discussion on "<code> tags" within the PHP Forum section. This forum, and the thread "<code> tags are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jul 5th, 2007, 10:03
Highly Reputable Member
Join Date: Jun 2007
Location: Canterbury
Age: 20
Posts: 726
Thanks: 0
Thanked 0 Times in 0 Posts
<code> tags

Hi all,

I've written a CMS for my blog site, and I'm trying to write a simple tutorial.

However, I can't work out how to add html code to the page without the engine rendering it as html.

So for example if I type into my blog post box...

HTML: Select all
<p style="font-weight: bold;">Spike</p>
Then it would display the word Spike in Bold.

But what if I wanted this to display as code as typed above in my post without it actually rendering the html?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jul 5th, 2007, 11:18
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

Quote:
Originally Posted by 1840dsgn View Post
But what if I wanted this to display as code as typed above in my post without it actually rendering the html?
There are two functions in PHP, called htmlentities and htmlspecialchars.
htmlspecialchars takes a string and replaces the symbols < > & ' " with their HTML representation, i.e. < becomes &lt;
htmlentities is more general in that it replaces all symbols in a string with the HTML entities. For example € becomes &euro; and £ becomes &pound;

So, to display a string as code you just write
Code: Select all
echo htmlentities($string);
Hope that helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jul 5th, 2007, 11:36
Highly Reputable Member
Join Date: Jun 2007
Location: Canterbury
Age: 20
Posts: 726
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

Yes you understand what I mean.

But my porblem is...

Basically my CMS allows me to type the text for the post into a textbox. As I am doing right now in this post.

And here you use [ code ] and [ /code ] and anything in that bracket will be displayed correctly.

I need a similar thing.

If I post this in my blog:
PHP: Select all

<h1>Spike</h1>

<?php echo htmlentities('<h1>Spike</h1>'); ?>
It does not work because the php has already been processed so this bit doesn't get processed...

do you understand?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jul 5th, 2007, 12:02
Most Reputable Member
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

Have you tried the code element?

Like this:
Code: Select all
<code><p style="font-weight: bold;">Spike</p></code>
Should work, I think.

Pete.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jul 5th, 2007, 12:12
Most Reputable Member
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

That doesn't work, I just tried it.

Pete.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jul 5th, 2007, 12:13
Most Reputable Member
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

Use the funny symbols. &alt; etc. That should work.

Pete.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jul 5th, 2007, 12:15
Highly Reputable Member
Join Date: Jun 2007
Location: Canterbury
Age: 20
Posts: 726
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

yea I have done that now.

its a bugger though.

Cos when I go to edit the post, it replaces all the funny symbols with what they represent so I have to go through replacing them all again!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jul 5th, 2007, 12:23
Most Reputable Member
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

A combination is probably best.

Like this:

Code: Select all
<pre>    
<code>&lt;html&gt;</code>
    <code>&lt;head&gt;</code>
        <code>&lt;title&gt;Title&lt;title&gt;</code>
    <code>&lt;/head&gt;</code>
    <code>&lt;body&gt;</code>
    <code>&lt;/body&gt;</code>
<code>&lt;/html&gt;</code>    
</pre>
Doing this even respects the indents you make so this basic html structure will actually display with all of the indents without any trickery. Cool huh?

Hope that works,

Pete.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jul 5th, 2007, 12:54
Highly Reputable Member
Join Date: Jun 2007
Location: Canterbury
Age: 20
Posts: 726
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

Thanks Pete thats excellent!

I've still got the problem that when I then go back in to edit it, the
Code: Select all
&lt; and &gt;
are displayed as < and > so i have to go through editing this.

if anyone knows how I can combat this please speak up!

To explain how it works...

I enter the post in a form...it is sent and stored in mySQL db.

When I go to edit it it is retrieved from the db and displayed in the form fields using GET queries.

So...there ya go!

Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jul 5th, 2007, 12:58
Most Reputable Member
Join Date: May 2006
Location: North West, UK
Age: 22
Posts: 1,173
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

I would imagine you'll have change your cms in some way though I'm not sure how. It is also very irritating just writing those &lt; things out. The example above took me ages.

I wonder how they do it on forums, I know forums are pretty complex but someone must know how it's done.

Pete.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jul 9th, 2007, 13:20
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

I had the same problem with my custom CMS

When you go to edit, display the content with the htmlentities() function hschmitz told you about
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jul 9th, 2007, 17:08
Highly Reputable Member
Join Date: Jun 2007
Location: Canterbury
Age: 20
Posts: 726
Thanks: 0
Thanked 0 Times in 0 Posts
Re: <code> tags

Cool thanks Karrine works brilliantly. I thought it might cancel out all html tags but it only does those between the code tags! Excellent.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
code php html

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Title Tags, Meta Tags and SEO - Keyword Density or Keyword Spamming? pctank Search Engine Optimization (SEO) 2 Jul 4th, 2008 11:52
alt tags in css multichild Web Page Design 1 Feb 24th, 2008 12:35
code behind code doesnt work skat ASP.NET Forum 4 Feb 18th, 2008 10:05
live search code and styleswitcher code hebel JavaScript Forum 0 May 12th, 2007 06:16
Can somebody give me the code to hide the source code? renren JavaScript Forum 7 Mar 7th, 2006 12:27


All times are GMT. The time now is 03:31.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42