Ankündigung

Einklappen
Keine Ankündigung bisher.

Problem mit preg_replace_callback

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Problem mit preg_replace_callback

    Ich habe mit meinem kleinen Parser ein kleines Problem. Er streikt bei diesen Sachen hier:

    Fehlermeldung:
    Warning: preg_replace_callback() requires argument 2, 'getContent', to be a valid callback in C:\Server\www\ws\inc\class.inc.php on line 37

    /* inc/class.inc.php */
    Code:
    <?php
    
    class Parser
    {
    
        /* ... */
    
        /* getFile() - liest die .html Datei aus */
        /* Display() - ruft Standards() auf und return´d die geparste Datei */
    
    	function getContent ($file)
    	{
    		ob_start();
    		include($file);
    		return ob_get_content();
    	}
    	
    	function Standards ()
    	{	
    		$output = $this->input;
    		$output = preg_replace('/{date:(.+)}/e', 'date(\'$1\');', $output);
    		$output = preg_replace_callback('/{include:(.+)}/e', 'getContent', $output); // line 37
    		$this->input = $output;
    		return $this->input;
    	}
    
        /* ... */
    
    }
    
    ?>
    /* index.php */
    Code:
    <?php
    
    include('inc/class.inc.php');
    $main = new Parser();
    $main->getFile('main.html');
    echo $main->Display();
    
    ?>
    /* main.html */
    Code:
    <html>
    <head>
     <title>test</title>
    </head>
    <body>
    {include:test.html}
    </body>
    </html>

  • #2
    Code:
    $output = preg_replace_callback('/{include:(.+)}/e', array($this,'getContent'), $output); // line 37

    Kommentar


    • #3
      Naja... will auch ned...
      Code:
      		$output = preg_replace_callback('/{include:(.+)}/e', array($this,'getContent'), $output); // line 37
      mit dem delemiter /e gehts ned...
      Warning: /e modifier cannot be used with replacement callback in C:\Server\www\ws\inc\class.inc.php on line 38
      dann hab ichs mal mit /si (oder nur "/") getestet:
      Code:
      		$output = preg_replace_callback('/{include:(.+)}/si', array($this,'getContent'), $output); // line 37
      nun kommt:
      Notice: Array to string conversion in C:\Server\www\ws\inc\class.inc.php on line 29

      Warning: getcontent(Array): failed to open stream: No such file or directory in C:\Server\www\ws\inc\class.inc.php on line 29

      Warning: getcontent(): Failed opening 'Array' for inclusion (include_path='.;c:\php4\pear') in C:\Server\www\ws\inc\class.inc.php on line 29


      Notice: Array to string conversion in C:\Server\www\ws\inc\class.inc.php on line 29

      Warning: getcontent(Array): failed to open stream: No such file or directory in C:\Server\www\ws\inc\class.inc.php on line 29

      Warning: getcontent(): Failed opening 'Array' for inclusion (include_path='.;c:\php4\pear') in C:\Server\www\ws\inc\class.inc.php on line 29
      // Edit:
      wieso denn überhaupt Array() und was is $this?

      Kommentar


      • #4
        $this ist der Verweis auf das Objekt ..

        $this->getContent();

        So würdest du es innerhalb des Objekts auch selbst aufrufen.
        Als Callback wird das ebenfalls so gemacht, allerdings übergibst du mit $this erstmal eine Kopie .. mit &$this gibst du eine Referenz weitern (bessere Idee)


        In getContent() sollte du dir mal über
        print '<pre>';
        var_dump($file);
        print '</pre>';
        anschauen, was da übergeben wird. ist nämlich nicht, was du denkst ^^

        Kommentar


        • #5
          okay indirekt hab ichs nun, wenn ich include($file[1]) mache, weil $file[0] = "{include:test.html}" ist...
          Nur ich bekomm den Inhalt der test.html 2 mal ausgegeben :!

          // Edit:
          bei so einer test.html:
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
           <title>Moin</title>
          </head>
          <body>
           {date:d.m.Y}
          
           {include:templates/test.html}
          </body>
          </html>
          bekomm ich:

          fdshdndyd 07.07.2004
          fdshdndyd
          also ist das 1ste falsch... (fdshds.. is der inhalt der test.html :P)

          // Edit 2:
          bei mehreren {include:...}´s kommt:
          Warning: getcontent(templates/test.html}

          {include:templates/test.html): failed to open stream: No such file or directory in C:\Server\www\ws\inc\class.inc.php on line 29

          Warning: getcontent(): Failed opening 'templates/test.html}

          {include:templates/test.html' for inclusion (include_path='.;c:\php4\pear') in C:\Server\www\ws\inc\class.inc.php on line 29
          und nen var_dump auf $file bringt das:
          array(2) {
          [0]=>
          string(76) "{include:templates/test.html}



          {include:templates/test.html}"
          [1]=>
          string(66) "templates/test.html}



          {include:templates/test.html"
          }
          alias ich kann das $file[1] nicht lassen aber irgendwas muss ich da doch machen :!

          Kommentar

          Lädt...
          X