Jump to content

Blog - PHPBB3


gaza165

Recommended Posts

Hi I made a class: Seems to work fine for most:

 

<?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[1][$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]

 

Basic gist of how to use:

 

$phpposter = new Phpposter();
echo $phpposter->highlight_code($blogpost);

Link to comment
Share on other sites

In a moment someone will tell you that you shouldn't use PHP4-style OOP. They'll also tell you that you should use the setter methods you've created and that you should just set default values instead of doing it in the constructor. Moreover, you'll be told that instead of tracking "changes" you should just use preg_replace_callback() to replace matches.

Link to comment
Share on other sites

Oh I know that the code is not without its fault. Oh and the PHP 4-style constructor is a habit I have from Java. The class was kind of rushed together. It works and I use it personally; so I wont lose any sleep over it. I'll keep what you said about what I didn't know in mind though. 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.