par
ramy » 22 déc. 2006, 03:06
Bonjour,
je rencontre un probleme avec un code et nottament la fonction html_entity_decode. Voici le code en question :
static function highlight($str) {
$str = html_entity_decode($str);
$str = highlight_string($str, true);
$str = preg_replace(
array('`<(?:font color="|span style="color: )'.ini_get('highlight.html').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.comment').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.default').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.keyword').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.string').'">(.+?)</(?:font|span)>`si',
'`</(?:font|span)>`si'),
array('<span style="color:'.ini_get('highlight.html').';">$1</span>',
'<span style="color:'.ini_get('highlight.comment').';">$1</span>',
'<span style="color:'.ini_get('highlight.default').';">$1</span>',
'<span style="color:'.ini_get('highlight.keyword').';">$1</span>',
'<span style="color:'.ini_get('highlight.string').';">$1</span>',
'</span>'),
$str);
return $str;
}
Dans le cas présent, ce code est appelé par une autre fonction static de la meme classe sous la forme (en gras dans ce code):
static public function Format($str) {
$str = str_replace('\\','',$str);
$support = array('p'=>'p',
'ul'=>'ul',
'ol'=>'ol',
'li'=>'li',
'h1'=>'h3',
'h2'=>'h4',
'h3'=>'h6',
'i'=>'i',
'b'=>'strong',
'strong'=>'strong',
'br'=>'br/',
'br/'=>'br/',
'code'=>'code');
foreach ($support as $t=>$v)
$str = str_replace(array("<$t>","</$t>"),
array("<$v>","</$v>"),
$str);
// highlight <code>
$str = preg_replace('/<code>(.*?)<\/code>/si',
[b]self::highlight('$1')[/b],
$str);
return $str;
}
Or je ne parviens pas avec ce script au résultat voulu qui est de coloriser un code html quelconque. Ce code est passé avant son insertion dans le SGBD par htmlentities; Il convient donc de transformer les entités html de celui-ci. Le probleme ne vient pas en soit de la fonction html_entity_decode puisque le résultat n'est pas plus probant avec str_replace. Ce qui me surprant le plus est que je ne parviens pas à voir l'état de la chaine de caratere $str dans la fonction highlight avec die() nottament : Lorsque je place un die($str) entre 2 séquences de cette fonction, elle ne me renvoit qu'un maigre $1. En revanche, elle est tout à fait fonctionnelle dans les meme condictions mais hors de son contexte. Constatez-vous une erreure dans ce code? Auriez vous des idées, des remarques, des solutions?
Merci,
ramy.
PS : Voici un exemple de chaine a formater via cette méthode :
Code : Tout sélectionner
<html>
<head>
<title>1ere page php</title>
</head>
<body>
Une phrase statique
<script language="PHP">
print "Une phrase dynamique.";
</script>
Une nouvelle phrase statique.
<?php
print "Une nouvelle phrase dynamique";
?>
</body>
</html>
Bonjour,
je rencontre un probleme avec un code et nottament la fonction html_entity_decode. Voici le code en question :
[php] static function highlight($str) {
$str = html_entity_decode($str);
$str = highlight_string($str, true);
$str = preg_replace(
array('`<(?:font color="|span style="color: )'.ini_get('highlight.html').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.comment').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.default').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.keyword').'">(.+?)</(?:font|span)>`si',
'`<(?:font color="|span style="color: )'.ini_get('highlight.string').'">(.+?)</(?:font|span)>`si',
'`</(?:font|span)>`si'),
array('<span style="color:'.ini_get('highlight.html').';">$1</span>',
'<span style="color:'.ini_get('highlight.comment').';">$1</span>',
'<span style="color:'.ini_get('highlight.default').';">$1</span>',
'<span style="color:'.ini_get('highlight.keyword').';">$1</span>',
'<span style="color:'.ini_get('highlight.string').';">$1</span>',
'</span>'),
$str);
return $str;
}
[/php]
Dans le cas présent, ce code est appelé par une autre fonction static de la meme classe sous la forme (en gras dans ce code):
[php]
static public function Format($str) {
$str = str_replace('\\','',$str);
$support = array('p'=>'p',
'ul'=>'ul',
'ol'=>'ol',
'li'=>'li',
'h1'=>'h3',
'h2'=>'h4',
'h3'=>'h6',
'i'=>'i',
'b'=>'strong',
'strong'=>'strong',
'br'=>'br/',
'br/'=>'br/',
'code'=>'code');
foreach ($support as $t=>$v)
$str = str_replace(array("<$t>","</$t>"),
array("<$v>","</$v>"),
$str);
// highlight <code>
$str = preg_replace('/<code>(.*?)<\/code>/si',
[b]self::highlight('$1')[/b],
$str);
return $str;
}
[/php]
Or je ne parviens pas avec ce script au résultat voulu qui est de coloriser un code html quelconque. Ce code est passé avant son insertion dans le SGBD par htmlentities; Il convient donc de transformer les entités html de celui-ci. Le probleme ne vient pas en soit de la fonction html_entity_decode puisque le résultat n'est pas plus probant avec str_replace. Ce qui me surprant le plus est que je ne parviens pas à voir l'état de la chaine de caratere $str dans la fonction highlight avec die() nottament : Lorsque je place un die($str) entre 2 séquences de cette fonction, elle ne me renvoit qu'un maigre $1. En revanche, elle est tout à fait fonctionnelle dans les meme condictions mais hors de son contexte. Constatez-vous une erreure dans ce code? Auriez vous des idées, des remarques, des solutions?
Merci,
ramy.
PS : Voici un exemple de chaine a formater via cette méthode :
[code]
<html>
<head>
<title>1ere page php</title>
</head>
<body>
Une phrase statique
<script language="PHP">
print "Une phrase dynamique.";
</script>
Une nouvelle phrase statique.
<?php
print "Une nouvelle phrase dynamique";
?>
</body>
</html>
[/code]