I'm not sure if anyone can understand this, if you need more info, just tell me
[SOLVED]preg_replace pass to variable
Moderator: General Moderators
[SOLVED]preg_replace pass to variable
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
I'm not sure if anyone can understand this, if you need more info, just tell me
Last edited by vigge89 on Thu Sep 09, 2004 7:54 am, edited 1 time in total.
Thanks alot 
I got a new problem though, I recieve this error when using the follwoing code:
what's wrong? :/
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 121Code: 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 ();
}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:
and here's the output of (no spaces in tags)
[ code ]
<?php
echo "hello";
?>
[/ code ]
( <?phpnArrayn?> )
any ideas on why it returns a n, Array and after that, and n?
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;
}
}[ code ]
<?php
echo "hello";
?>
[/ code ]
Code: Select all
<span class='keyword'><?</span><span class='default'>phpnArrayn?></span>any ideas on why it returns a n, Array and after that, and n?