Jump to content

Class that formats the PHP that I post inside [code] tags on my blog not working


waynew

Recommended Posts

For some reason, it will only place the first code block inside a div and leave the rest un-formatted:

 

Here's the class:

 

class Phpposter{

	var $border;
	var $padding;
	var $width;
	var $margin_bottom;
	var $bg_color;
	var $font_size;

	function Phpposter(){
		$this->border = "1px #CCCCCC solid";
		$this->width = "500px";
		$this->margin_bottom = "5px";
		$this->padding="3px";
		$this->bg_color="#FFFFFF";
		$this->font_size="14px";
	}

	function highlight_code($string){
		//We're looking for [code] tags here. 
		$pattern = "/\[code\](.*?)\[\/code\]/is";
		preg_match($pattern, $string, $matches); //Find all matches
		$changes = array(); //This array will hold all the changes we made. These changes will replace the matches.

		if(sizeof($matches) < 1){ //If no code tags are found, return the string as it was
			return $string;
		}

		else{
			foreach($matches as $match){
				array_push($changes,$this->style($match));//Fill up changes array
			}
		}


		$i=0;//counter
		while($i < sizeof($matches)){
			$string = str_replace($matches[$i],$changes[$i],$string);
			$i++;
		}

		$string = str_replace("[code]","",$string);
		$string = str_replace("

","",$string);

 

return $string;

 

}

 

function style($string){

 

$div =

'<div style="background-color:'.$this->bg_color.';

width:'.$this->width.';

padding:'.$this->padding.';

border:'.$this->border.';

margin-bottom:'.$this->margin_bottom.';

font-size:'.$this->font_size.';

white-space: pre;

overflow: auto;">';

 

$string = str_replace("<br />","",$string);

$string = str_replace("<br/>","",$string);

$string = str_replace("\n","",$string);

 

$string = '<br/>'.$div.highlight_string($string,true).'</div>'; //Place div around code to make it stand out

return $string;

 

}

 

function set_width($width){

$this->width=$width;

}

 

function set_margin_bottom($margin_bottom){

$this->margin_bottom = $margin_bottom;

}

 

function set_border($border){

$this->border = $border;

}

 

function set_font_size($font){

$this->font_size = $font;

}

 

function set_bg_color($bg){

$this->bg_color = $bg;

}

 

function set_padding($padding){

$this->padding = $padding;

}

}

 

?>

[/code]

Link to comment
Share on other sites

LOL I have this now, but as you can see, its gone a bit haywire. XD http://waynewhitty.com/blog.php?post=3&category=1

 

<?php

class Phpposter{

	var $border;
	var $padding;
	var $width;
	var $margin_bottom;
	var $bg_color;
	var $font_size;

	function Phpposter(){
		$this->border = "1px #CCCCCC solid";
		$this->width = "500px";
		$this->margin_bottom = "5px";
		$this->padding="3px";
		$this->bg_color="#FFFFFF";
		$this->font_size="14px";
	}

	function highlight_code($string){
		//We're looking for [code] tags here. 
		$pattern = "/\[code\](.*?)\[\/code\]/is";
		preg_match_all($pattern, $string, $matches); //Find all matches
		$changes = array(); //This array will hold all the changes we made. These changes will replace the matches.

		if(sizeof($matches) < 1){ //If no code tags are found, return the string as it was
			return $string;
		}

		else{
			foreach($matches[1] as $match){
				array_push($changes,$this->style($match));//Fill up changes array
			}
		}


		$i=0;//counter
		while($i < sizeof($matches)){
			$string = str_replace($matches[$i],$changes[$i],$string);
			$i++;
		}

		$string = str_replace("[code]","",$string);
		$string = str_replace("

","",$string);

 

return $string;

 

}

 

function style($string){

 

$div =

'<div style="background-color:'.$this->bg_color.';

width:'.$this->width.';

padding:'.$this->padding.';

border:'.$this->border.';

margin-bottom:'.$this->margin_bottom.';

font-size:'.$this->font_size.';

white-space: pre;

overflow: auto;">';

 

$string = str_replace("<br />","",$string);

$string = str_replace("<br/>","",$string);

$string = str_replace("\n","",$string);

 

$string = '<br/>'.$div.highlight_string($string,true).'</div>'; //Place div around code to make it stand out

return $string;

 

}

 

function set_width($width){

$this->width=$width;

}

 

function set_margin_bottom($margin_bottom){

$this->margin_bottom = $margin_bottom;

}

 

function set_border($border){

$this->border = $border;

}

 

function set_font_size($font){

$this->font_size = $font;

}

 

function set_bg_color($bg){

$this->bg_color = $bg;

}

 

function set_padding($padding){

$this->padding = $padding;

}

}

 

?>

[/code]

Link to comment
Share on other sites

Sorry should have noticed the other parts as well.

 

This:

while($i < sizeof($matches)){
$string = str_replace($matches[$i],$changes[$i],$string);
$i++;
}

 

Should be:

while($i < sizeof($matches[1])){
$string = str_replace($matches[1][$i],$changes[$i],$string);
$i++;
}

Link to comment
Share on other sites

Here's the blogpost itself:

 

If you're wanting to create your own PHP login system, you're going to have to learn about sessions. Sessions basically allow your server to recognise the user who is browsing your website. For example, when you log in to your favourite social network or forum board, or email account, the website you've just logged into issues you with a session. Then, on each page, that website will check to see if you have a session. If you don't have a session, the website will assume that you are not logged in. If you do have a session, the website will assume that you are logged in. Simple enough to understand? Good! Lets get started.

 

Handling sessions in PHP is incredibly easy. So easy that you'll probably kick yourself for not learning about them sooner. Here are a few functions and variables that you will use day in and day out when programming with sessions in mind.

 

<?php 
session_start( );
$_SESSION['example'];
session_unregister( );
session_destroy( );
?>

 

session_start( ) is a function that is vital to you using sessions in your PHP scripts.

This function should be placed at the top of EVERY single one of your pages. It is what allows a session from one page to carry over to the next. Without it, you'll be in a world of hurt. Other than that, at this stage, you don't really need to know that much about it. Just remember that it should be the first line on every single one of your pages.

 

$_SESSION['example'], in this case, is a session variable named "example". You can call it whatever you like though. It is a variable that will hold anything that you might like to add to your sessions, such as a user's username, name or age etc.

This variable DOES NOT need to be re-declared on each page. As long as you have session_start( ); at the top of your pages, session variables such as the one I mentioned above will carry their values from page to page. Lets see a small example:

 

<?php
session_start( ); //remember, this must be a the top
$_SESSION['name'] = "Wayne";
?>

 

As you can see in above script, I have assigned the name Wayne to a session variable called "name". Then on another page, I can simply print this variable out like so:

 

<?php
session_start( ); //yes! at the top
echo $_SESSION['name'];
?>

 

You see, as long as I continue to use session_start( ); at the top of my pages, I can print out session variables that were created on another page. Magic eh? It is probably worth noting that you can have any number of session variables in existence at the exact same time. So, for example, when a user logs into your website, you could give them a number of sessions variables like so:

 

<?php
session_start( );
$_SESSION['name'] = $username;
$_SESSION['age'] = $age;
$_SESSION['login_time'] = mktime( );
$_SESSION['location'] = $location;
?>

 

session_unregister( ) is a function that allows you to unregister one session variable, but leave the rest in existence. For example, working off the script I gave above, say I wanted to get rid of the session variable "location", for whatever reason. I would simply do something like this:

 

<?php
session_start( );
session_unregister($_SESSION['location']);
?>

 

I'll be honest and say that you'll probably rarely only need to use session_unregister( ). If you're looking to update a session, simply overwrite the value that is already there:

 

<?php
session_start( );
$_SESSION['name'] = "Wayne"; //variable "name" is Wayne
$_SESSION['name'] = "John"; //variable "name" is now John
?>

 

Now for session_destroy( ). Ever wonders what happens behind the scenes when you log out? Well, now you know. Let's create a simple logout script.

 

<?php
session_start( ); //yes, even on the logout script
session_destroy( ); //you don't need to give this function anything
header('Location: login.php'); //redirect to login page
?>

 

The above script will destroy a users session using PHP.

 

So, how do you check to see if somebody is logged in or not? Well, simply do it like so:

 

<?php
session_start( ); //bored of this function yet?
if(!isset($_SESSION['name'])){
     header('Location: login.php');
}
else{
     //show page
}
?>

 

With the above script, the PHP checks to see if a session variable called "name" has been set and is in existence. If it is not in existence, the system redirects them back to a page called login.php. If the session variable is in existence, the page is shown.

 

Well, thats it for the very basics. Hope it helps you get started with sessions. If you have any advice or questions, please post a comment below. Thanks.

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.