replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alexjaquet@gmail.com

    replace

    Hi,

    I'm looking for a way to replace some variables for example in perl I
    do :

    #replace all $LABEL variables with corresponding values from $SERVER
    var
    s/\$LABEL{'([\w]+)'}/ exists $SERVER{$1} ? $SERVER{$1} : $1 /eg;
    #replace $LANG var with the value of $lang
    s/\$LANG/$lang/g;

  • Justin Koivisto

    #2
    Re: replace

    alexjaquet@gmai l.com wrote:[color=blue]
    > Hi,
    >
    > I'm looking for a way to replace some variables for example in perl I
    > do :
    >
    > #replace all $LABEL variables with corresponding values from $SERVER
    > var
    > s/\$LABEL{'([\w]+)'}/ exists $SERVER{$1} ? $SERVER{$1} : $1 /eg;
    > #replace $LANG var with the value of $lang
    > s/\$LANG/$lang/g;
    >[/color]

    What is the context of this? Are you talking about replacing literal
    "$LABEL{'label_ name'}" with the value of _$SERVER['label_name'] in a string?

    If so, you could try this where $s is the string that has the content
    that will need the replacing:

    <?php
    $pattern='/\$LABEL\{\'([\w]+)\'\}/s';
    if(preg_match_a ll($pattern,$s, $m)){
    $matches=count( $m[0]);
    for($i=0;$i<$ma tches;$i++){
    if(isset($_SERV ER[$m[1][$i]])){
    $s=str_replace( $m[0][$i],$_SERVER[$m[1][$i]],$s);
    }else{
    $s=str_replace( $m[0][$i],$m[1][$i],$s);
    }
    }
    }
    ?>

    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • alexjaquet@gmail.com

      #3
      Re: replace

      thanks Justin for responding

      -What is the context of this?

      I want to open an html file, parse it and replace all
      $LABEL{'label_n ame'} variables by the key values pairs who are placed
      in an config config file I do this to *delete php code* in html page
      (MVC2)

      for example to store in an hash table the keys values I do :

      sub loadLanguage {
      $lang = $query->param("lang" );
      $lang = uc ($lang);
      open (FILE, "<$dirLang/$lang.conf") or die "cannot open file
      $dirLang/$lang.conf";
      while (<FILE>) {
      (local our $label, local our $value) = split(/=/);
      $SERVER{$label} = $value;
      }
      close (FILE);
      }

      Comment

      • alexjaquet@gmail.com

        #4
        Re: replace

        then I open the corresponding page ( I don't want to change the way I
        work in perl I want to do it in php right now)):

        sub loadMixTape {
        open (FILE, "<$dir/mix_tape.html") or die "cannot open file
        $dir/mix_tape.html";
        local our $menu = loadMenu();
        my $string4 = weekNews ();
        local our $cats = getCat();
        local our $content;
        while (<FILE>) {
        s/\$LABEL{'([\w]+)'}/ exists $SERVER{$1} ? $SERVER{$1} : $1 /eg;
        s/\$LANG/$lang/g;
        s/\$OPTIONS{'cate gories'}/$cats/g;
        s/\$ERROR{'([\w]+)'}//g;
        s/\$ARTICLE{'main _menu'}/$menu/g;
        s/\$ARTICLE{'sear ch'}//g;
        s/\$ARTICLE{'week _news'}/$string4/g;
        s/\$LINK{'admin'}/$LINK{'admin'}/g;
        s/\$SESSIONID/$session_id/g;
        $content .= $_;
        }
        $content = Compress::Zlib: :memGzip($conte nt) if $can_do_gzip; ;
        print "Content-Length: ", length($content ) , "\n";
        print "Content-Encoding: gzip\n" ;
        print "Content-Type: text/html\n\n";
        print $content;
        close (FILE);
        }

        Comment

        Working...