SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regexp help needed - problems with /e

    Hello,

    I have this regexp:

    PHP Code:
    $search = array(
       
    '000000' => '',
       
    'FF9900' => 'comment'
       
    '007700' => 'syntax1',
       
    '0000BB' => 'syntax2'
       
    'DD0000' => 'strings'
    );

    $output preg_replace(
       
    '!<font color="#(000000|FF9900|007700|0000BB|DD0000)">(.+?)</font>!ise',
       
    '<span class="\$replace[$1]">$2</span>',
       
    $output
    ); 
    $replace[$1] grabs the css class from the array. The problem is that because of the /e switch PHP treats '<span ...' as a function and the regexp fails.
    I have been trying to fix this for some hours now but couldn't find the correct solution.

    Thanks in advance,
    Christian

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Christian, I am at a loss here. I messed around with your regex and couldn't come up with a solution unfortunately. Hopefully, someone else a little more than sub par with regular expressions will stumble along and help you out. I should really read trhough my copy of "Master Regurar Expressions"

    For the meantime, a solution such as the following can substitute for the eval solution. It's a little longer, but you can dynamically build the patterns and replacements arrays. Still, not really the ideal solution, but workable.

    PHP Code:
    $patterns[0] = "!<font color=\"#FF9900\">(.+?)</font>!is";
    $patterns[1] = "!<font color=\"#007700\">(.+?)</font>!is";
    $patterns[2] = "!<font color=\"#0000BB\">(.+?)</font>!is";
    $patterns[3] = "!<font color=\"#DD0000\">(.+?)</font>!is";
    $patterns[4] = "!<font color=\"#000000\">(.+?)</font>!is";
    $replacements[0] = "<span class=\"comment\">$1</span>";
    $replacements[1] = "<span class=\"syntax1\">$1</span>";
    $replacements[2] = "<span class=\"syntax2\">$1</span>";
    $replacements[3] = "<span class=\"strings\">$1</span>";
    $replacements[4] = "<span class=\"\">$1</span>";

    $output preg_replace
       
    $patterns
       
    $replacements
       
    $output 
    ); 
    Good luck in finding real answer to your question.

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $replace[$1]

    Shouldn't this be $search[$1]?
    John

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andrew, yes that's it what I was doing when I couldn't get it to work properly. The only problem of the regexp is the replacement part.
    PHPJohn that was a copy & paste error.

    I have now found the solution: I created a second function and pass the captured results as arguments to this function:

    PHP Code:
    function convert($class$text) {
        
    // to be used with with the /e switch later
        
    static $search = array(
            
    '000000' => '',
            
    'FF9900' => 'comment'
            
    '007700' => 'syntax1',
            
    '0000BB' => 'syntax2'
            
    'DD0000' => 'strings'
        
    );
        
        return 
    '<span class="' $search[$class] . '">' stripslashes($text) . '</span>'
    }

    function 
    php_highlight_file($filename) {
        
    // check if the file exists    
        
    if (!file_exists($filename))
            return;
            
        
    // use PHP's highlight_file
        
    $output highlight_file($filename1);
        
        
    $output preg_replace(
            
    '!<font color="#(000000|FF9900|007700|0000BB|DD0000)">(.+?)</font>!ise',
            
    "convert('$1', '$2')",
            
    $output);
            
        return 
    $output;
        


  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad to hear everything worked out. I can't believe I overlooked the variable mismatch


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
  •