SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Iasi, Romania
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pattern from preg_replace as an argument to outside function

    I am trying to use preg_replace with a function to process the variable between replacements. The final scope is to improve a rtf generator function adding support for images. I know the class in original form is ok (taken from phpclasses.org) so the problem is what I am trying to do:

    The input is like Text here<image>foo.jpg</image>another text here<image>bar.jpg</image>. The purpose is to identify <image> tags, strip them and process the images with a function

    PHP Code:
     
    class rtf {
     
    //some code goes here ...
     
     
     
    function addImageCode($img) {
     
    //write some code using image
     
    return $code;
     
    }
     
     
     
    function 
    parseDocument() {
     
     
     
    $doc_buffer preg_replace("/\<image\>(.+?)\<\/image\>/msie""addImageCode(\"$1\")",$doc_buffer);
     
    return 
    $doc_buffer;
     
    }
     
     
     



    but running it I receive a: Fatal error: Call to undefined function addImageCode() in /home/mysite/rtf/class_rtf.php(414) : regexp code on line 1

    If I move addImageCode function inside parseDocument() function it works but I do not consider as a good programimg practice.

    Any ideeas?

    class is called like:
    PHP Code:
    $rtf = new rtf("rtf_config.php");
    $rtf->addText("$txt");
    $rtf->getDocument(); 
    the class method for output is:
    PHP Code:
    // Ouput the RTF file
     
    function getDocument() {
      
    $this->buffer .= "{";
      
    // Header
      
    $this->buffer .= $this->getHeader();
        
    // Parse the text into RTF
      
    $this->buffer .= $this->parseDocument();
      
    $this->buffer .= "}";
      
      
    header("Content-Type: text/enriched\n");
      
    header("Content-Disposition: attachment; filename=rtf.rtf");
      echo 
    $this->buffer;
     } 

  2. #2
    SitePoint Zealot shaman's Avatar
    Join Date
    Feb 2005
    Location
    W.R. Home
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    case addImageCase() is method of your class, but not simple function.
    I dont know should it work or not, but maybe try some like $this->addImageCase() ?
    She's my german fraulein

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Iasi, Romania
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    doesn't work

    it doesn't work. I get:

    Parse error: parse error, unexpected T_OBJECT_OPERATOR in /home/mysite/rtf/class_rtf.php(414) : regexp code on line 1

    Fatal error: preg_replace() [function.preg-replace]: Failed evaluating code: this->addImageCode("358252.jpg") in /home/mysite/rtf/class_rtf.php on line 414

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are looking for this:
    PHP Code:
    $doc_buffer preg_replace_callback('~<image>(.+?)</image>~msi', array($this,'addImageCode'),$doc_buffer); 
    Note that the $img you receive there will be the array of matches, so you will likely want to deal with $img[1];
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Iasi, Romania
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to do it further ?

    Although syntactically correct I thing your preg_replace_callback won't do its job. The logic here is:
    1. go through the document
    2. find an image (<image>foo.jpg</image>)
    3. strip the <image> and </image> tags and pass the foo.jpg to addImageCode function (different function in the same class) that will write something in the image place (the rtf code for image).
    4. go further through text
    5. find another image (<image>bar.jpg</image>)-> go to step 3.

    If I parse the document and obtain an array I don't know where to place the correct array elements in the correct places (where there were initially the <image> tags).
    So the ideea is that the finding, stripping tags and write the external function result have to be done at the same parsing time.
    Thanks for any ideeas.

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is exactly what it does.

    PHP Code:
     class rtf {
         protected 
    $doc;
         function 
    newDoc($doc) { $this->doc $doc; }
         function 
    addImageCode($img) {
             if (
    $img) {
                 
    preg_match('~([^\\/]*)\.(?:jpg|gif|png)$~i',trim($img[1]),$matches);
                if (
    $matches) {
                    
    $image_name $matches[1];
                } else {
                    
    $image_name 'image';
                }
             }
             return 
    '<img src="'.$img[1].'">'.$image_name.'</img>';
         }
         function 
    process() {
             
    $this->doc preg_replace_callback('~<image>(.+?)</image>~msi', array($this,'addImageCode'),$this->doc); 
         }
         function 
    showDoc() {
             echo 
    $this->doc"\n";
         }
     }
     
     
    $r = new rtf;
     
     
    $r->newDoc('test<image>foo.jpg</image> blah <image>zaa.gif</image> zoom');
     
    $r->showDoc();
     
    $r->process();
     
    $r->showDoc(); 
    Produces:
    Code:
     test<image>foo.jpg</image> blah <image>zaa.gif</image> zoom
    test<img src="foo.jpg">foo</img> blah <img src="zaa.gif">zaa</img> zoom

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps what I meant by "array of matches" is the source of your confusion. It is the array of matches for the particular capture you are working on, with the first element containing the entire matched text (i.e. $img[0] = <image>blah.jpg</blah>) and the subsequent elements containing the matches you wanted base on the parentheses you included in the regex pattern (i.e. $img[1] = blah.jpg).

    The preg_replace_callback will call the rtf::addImageCode() method once for each match it found in the source document, passing the array of matches for that particular match to the method.

    HTH

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Iasi, Romania
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    you are right

    yes ... you are perfectly right. It is working now. I call the function like:
    PHP Code:
    function addImageCode($img) {
       
    $image $img[1];
       
    $image "/images/".$image;
       
    $size_of_image GetImageSize($image);
       
    //...and so on 
    Regards,
    zico


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •