Page 1 of 1

[SOLVED]preg_replace pass to variable

Posted: Wed Sep 08, 2004 2:16 pm
by vigge89
I'm trying to come up with a way to loop trough each match of a pattern with preg_replace or other regexp function, set the match to an variable, do some things with the variable and finally replace the match with the variable.

I'm not sure if anyone can understand this, if you need more info, just tell me ;)

Posted: Wed Sep 08, 2004 2:21 pm
by feyd
[php_man]preg_replace_callback[/php_man] or a regex with the execute modifier.

Posted: Wed Sep 08, 2004 2:29 pm
by vigge89
Thanks alot :)
I got a new problem though, I recieve this error when using the follwoing code:

Code: Select all

Warning: preg_replace_callback() їfunction.preg-replace-callback]: requires argument 2, 'highlight', to be a valid callback in J:\Server\htdocs\mods\code_format.php on line 121

Code: Select all

$string = preg_replace_callback ("#\[code\](.*?)\[/code\]#si", "highlight", $string);

// function which highlights code and returns formatted output
function highlight ($code) {
	$highlight = New highlighter();
	$highlight->set_code ($code);
	return $highlight->process ();
}
what's wrong? :/

Posted: Wed Sep 08, 2004 3:17 pm
by vigge89
I got the error to disappear, but now I've got a new problem, the highlight-function seems to return an array, here's the code for the class:

Code: Select all

<?php

class highlighter {
	var $code;
	var $adjust;
	var $line_numbers;
	var $highlight_method;

############## HIGHLIGHTER - initiation
function highlighter ($method = 'highlight_string') {
	$this->highlight_method = $method;
	// set defaults
	$this->adjust = true;
	$this->line_numbers = true;
}

############## SET_CONFIG - set configuration
function set_config ($property, $value) {
	if (isset ($this->$property)) {
		$this->$property = $value;
		return true;
	} else {
		return false;
	}
}

############## SET_CODE - sets the code to highlight
function set ($code) {
	$this->code = $code;
}

############## CLEAR - empty code
function clear () {
	$this->code = '';
}

############## MAKE_CSS - create html/css code
function make_css ($code) {

	$code = preg_replace (
	'{([\w_]+)(\s*</span>)'.
	'(\s*<span\s+style="color: '.ini_get ('highlight.keyword').'">\s*\()}m',
	"<a class='function' href='http://www.php.net/$1'>$1</a>$2$3",
	$code);

	$code = preg_replace (
	'{([\w_]+)(\s*</font>)'.
	'(\s*<font\s+color="'.ini_get ('highlight.keyword').'">\s*\()}m',
	"<a class='function' href='http://www.php.net/$1'>$1</a>$2$3",
	$code);

	$colors[ini_get ('highlight.bg')] = 'background';
	$colors[ini_get ('highlight.comment')] = 'comment';
	$colors[ini_get ('highlight.default')] = 'default';
	$colors[ini_get ('highlight.html')] = 'html';
	$colors[ini_get ('highlight.keyword')] = 'keyword';
	$colors[ini_get ('highlight.string')] = 'string';

	foreach ($colors as $color=>$class) {
		$code = str_replace ('<span style="color: '.$color.'">', "<span class='$class'>", $code); // new highlighting
		$code = str_replace ('<font color="'.$color.'">', "<span class='$class'>", $code); // old highlighting
	}

	return str_replace ('</font>', '</span>', $code);
}

############## PROCESS - processes code and creates output
function process () {
$code = ($this->adjust && !strstr ($this->code, '<?php')) ? '<?php'.'\n'.$this->code.'\n'.'?>' : $this->code;
$hlfunc = $this->highlight_method;
$code = $hlfunc ($code, true);
$code = $this->make_css ($code);
$lines = count (explode ('<br />', $code));

$output = file_get_contents ('templates/code.tpl');

for ($i = 1; $i < ($lines + 1); $i++) {
	if($this->line_numbers) {
		$rownumbers .= "$i<br />";
	}
}

$output = str_replace ('{%rownumbers%}', $rownumbers, $output); // rownumbers
$output = str_replace ('{%code%}', $code, $output); // actual code
$output = str_replace ('{%numlines%}', $lines, $output); // number of lines

return $output;
}

}
and here's the output of (no spaces in tags)
[ code ]
<?php
echo "hello";
?>
[/ code ]

Code: Select all

&lt;span class='keyword'&gt;&lt;?&lt;/span&gt;&lt;span class='default'&gt;phpnArrayn?&gt;&lt;/span&gt;
( <?phpnArrayn?> )

any ideas on why it returns a n, Array and after that, and n?

Posted: Wed Sep 08, 2004 4:12 pm
by feyd
$this->code was an array.

Posted: Thu Sep 09, 2004 12:35 am
by vigge89
yeah, I thought so too, but it isn't, if i echo the outpout, it works, but not when I do it like this :(

Posted: Thu Sep 09, 2004 1:20 am
by feyd
maybe fixing the newlines in your code will help.. ;) (hint: double quotes)

I can tell you that preg_replace_callback sends an array to the function specified, always.

Posted: Thu Sep 09, 2004 7:54 am
by vigge89
yeah, i figured that out after some time smashing my head, it's solved now ;)
i'm probably gonna put the highlighting class up on my site if anyones intrested in it :)