[php] string_replace op highlight_string

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ik wil graag de <?php en ?> tekens verwijderen uit een string die ik wil weergeven met highlight_string().
Als ik de <?php en ?> nl. niet in de string zet dan krijg ik zwarte teks.
Ik had het volgende geprobeerd, maar tevergeefs.

PHP:
1
2
3
4
5
6
7
8
<?php
function php_string($line)
    {
        ob_start();
        str_replace((highlight_string ($line)), "<?php", " ");
        ob_end_flush();
    }
?>

Acties:
  • 0 Henk 'm!

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Kijk es naar preg_replace_callback

Acties:
  • 0 Henk 'm!

Verwijderd

kan je niet beter eerst deze tekens afvangen en daarna pas de highlight doen?

Acties:
  • 0 Henk 'm!

Verwijderd

zoiets dus:
PHP:
1
2
3
4
5
6
7
8
9
10
11
<?php 
function php_string($line) 
    { 
        ob_start(); 
        $line = str_replace("<?php",  " ", $line);
        $line = str_replace("<?",  " ", $line);
        $line = str_replace("?>",  " ", $line);
        highlight_string ($line);
        ob_end_flush(); 
    } 
?> 

Acties:
  • 0 Henk 'm!

  • beetle71
  • Registratie: Februari 2003
  • Laatst online: 09-09 15:24
Verwijderd schreef op 27 June 2003 @ 01:06:
zoiets dus:
PHP:
1
2
3
4
5
6
7
8
9
10
11
<?php 
function php_string($line) 
    { 
        ob_start(); 
        $line = str_replace("<?php",  " ", $line);
        $line = str_replace("<?",  " ", $line);
        $line = str_replace("?>",  " ", $line);
        highlight_string ($line);
        ob_end_flush(); 
    } 
?> 
Bijna goed ;) Maar bijna is niet helemaal.
PHP:
1
2
3
4
5
6
7
8
9
10
11
function showsource ($line) { 
  ob_start(); 
   highlight_string($line); 
   $colorsource = ob_get_contents(); 
   ob_end_clean(); 
   $colorsource = str_replace ("&nbsp;", " ", $colorsource); 
   $colorsource = str_replace ("&lt;?php", " ", $colorsource);
   $colorsource = str_replace ("?&gt;", " ", $colorsource); 
   return $colorsource; 
} 
echo showsource($line);


Zo werkt ie wel.

highlight_string() print zijn output direct naar de outputbuffer net zoals bijvoorbeeld var_dump() waardoor dit niet werkt
PHP:
1
$x=highlight_string($iets);


Dus zul je dit met ob_start(),ob_get_contents() en ob_end_clean(); moeten opvangen en in een variabele stoppen om daarna datgene eruit te halen dat eruit moet...
(en in de output is dat dus geen <?php maar <?php )

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Thanx beetle71, het werk prima. :)
Pagina: 1