strings aus file entfernen

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

  • strings aus file entfernen

    strings aus file entfernen

    suche ein script, c, vbasic oder php,
    um strings aus ner quellcode-datei zu entfernen/auszutauschen.

    dabei soll der suchstring durch ein teilstring am anfang und ein teilstring am ende definiert sein,
    wobei alles dazwischen unrelevant sein soll.

    Ein Beispiel zum vorstellen waere alle
    /*anfang_xxxxxxxxxx
    xxxxxxxxxx_ende*/
    comments entfernen.

    ich wuerde jetzt anfangen das
    file in string_all einlesen,
    Anfangsvorkommen suchen,
    von null bis dort ersten Teil-Ergebnisstring speichern,
    Endevorkommen suchen,
    ab da , bis ende oder naechstes Anfangsvorkommen speichern...
    die Teil-Ergebnisstrings zusammenfuegen,
    fertig.

    das gibt es doch sicherlich fertig?

    wie koennte ich s finden?
    Zuletzt geändert von andijota; 07.06.2006, 05:24.

  • #2
    das sind simple regex funktion die du auf in string gespeicherte inhalt von datei anwenden kannst und in geänderten zustand wieder speichern.
    in deinem fall wird regex so aussehen
    PHP-Code:
    error_reporting(E_ALL);

    $TXT = <<<Inhalt
    The Quick 15 Brown Fox Jumped Over The Lazy Dogs. 
    The Quick Brown /*anfang_xxxxxxxxxx 
    xxxxxxxxxx_ende*/  
     Fox Jumped Over The Lazy 30 Dogs.
    Inhalt;

    $pattern '#/\\*anfang_.*_ende\\*/#sU';
    $ersatz '';


    echo 
    preg_replace($pattern$ersatz$TXT); 
    Slava
    bituniverse.com

    Kommentar


    • #3
      genau! , DANKE

      Kommentar


      • #4
        tage spaeter:

        siehe next..
        Zuletzt geändert von andijota; 06.06.2006, 08:56.

        Kommentar


        • #5
          no comment ?

          no comment?
          hiermit komm ich jetzt jedenfalls zurecht ::

          pattern in array[] replace in file or in string
          and echo with line numbers


          zB.: gezielt bestimmte gekennzeichnete Kommentare in files entfernen


          // http://www.php-resource.de/manual.ph...n.preg-replace


          PHP-Code:
          <?php  //  repfile.php 1061
              
          $version1050 ' version: 1061
                  '
          ;
              
              
          error_reporting(E_ALL);
              
              global 
          $dbuggg 
              
          $dbuggg // echo or not 

              
          global $ar_bendrep 
              
          // array[] $ar_bendrep[] beinhaltet pro zeile drei strings:
              // find_muster_begin , findmuster_end , ersatzstring.
              
              
          $str_fil6 '' ;
              
          // file to string , enthaelt eingelesenes file 
              
              
          global $patfilnam6 
              
          // path/file
           
              // array $ar_bendrep[] in inc file // 
              // require_once('rep_oldnew_ar.php'); 
              
          $ar_bendrep = array(
                  
          '_beg_pattrn1_' '_end_pattrn1_' ' _repl1_ ' 
                  
          "_FULL_pattrn2_" "" " _repl2_ " 
                  
          "%%3" "3%%" " _repl3_ " ,
                  
          "§§" "§§" " _repl4_ " 
                  
          '\/\*' '\*\/' ''  // for DEL / * comment * /
                  
          '\/\/' chr(13).chr(10) , chr(13).chr(10)  , // for DEL // commnet
                  
          '\/\/' chr(10) , chr(13).chr(10)  , 
                  
          '\/\/' chr(13) , chr(13).chr(10)  , // chr(13) einsam kommt auch mal vor
                  // but this last , dont mutch?      
                  // schoen , ums letzte , zu viele komma braucht sich keiner kuemmern 
                  // "begin_patternYY" , "if_end_patternYY" , "replaceYY"  
              
          );
              
              
          // global $patfilnam6;
              
          $patfilnam6 '../../w048changedfiles/templates/original/css/original' ;   
              
          // /end in inc // require_once('rep_oldnew_ar.php'); 

              // teststring 
              
          if($dbuggg 2)
              {
                  
          $tststringg = <<<Txxxxt
                  ------ _beg_pattrn1_ one one one _end_pattrn1_ pause 
                  §§ and 4 multi §§ 
                  <br /> 
                  _FULL_pattrn2_ dont replace two /* two */ two  pause2 // comment2 // 2.2
                  <br /> 
                  %%3 three three three 3%% pause3 
                  <br /> 
                  §§4 and for last 4§§ ------
          Txxxxt;
              }

              
          $ix_ar_bendrep count($ar_bendrep);
              
              if(
          $dbuggg 2)
              {
                  
          $e_c_h_o  
                  
          'ar_bendrep[ix_ar='$ix_ar_bendrep .']: <br />'
                  
          .'beg[0]='.$ar_bendrep[0].' end[1]='.$ar_bendrep[1]
                  .
          ' rep[2]='.$ar_bendrep[2].'<br />'
                  
          .'beg[3]='.$ar_bendrep[3].' end[4]='.$ar_bendrep[4]
                  .
          ' rep[5]='.$ar_bendrep[5].'<br />' 
                  
          .'beg[6]='.$ar_bendrep[6].' end[7]='.$ar_bendrep[7]
                  .
          ' rep[8]='.$ar_bendrep[8].'<br />'
                  
          .'beg[9]='.$ar_bendrep[9].' end[10]='.$ar_bendrep[10]
                  .
          ' rep[11]='.$ar_bendrep[11].'<br />' ;
              }
                  
              
          $prc '' // pattern to find and replace
              
          for($ixx 0$ixx $ix_ar_bendrep ;  $ixx += 
              {
                  
          // if second search _end_ =="" then FULL_pattern string_replace like 
                  
          if('' == $ar_bendrep[1+$ixx]) $prc .= $ar_bendrep[$ixx]."|";
                  else 
          $prc .= $ar_bendrep[$ixx].".*?".$ar_bendrep[1+$ixx]."|"
              }
              
              function 
          format_endofline_to_1310$param_str_fil 
              {   
                  
          // waren teilweise nur chr(10) oder nur chr(13) als \newline  
                  
                 
          $control_chars 'QQ0_06600_7YY' // just sam chars  
                 // $tabspce = '   ' ; // 3
                 
          $tabspce '  ' // 2
                 
          $param_str_fil str_replacechr(13).chr(10),  $control_chars$param_str_fil );
                 
          $param_str_fil str_replacechr(10),  $control_chars$param_str_fil );
                 
          $param_str_fil str_replacechr(13),  $control_chars$param_str_fil );
                 
          $param_str_fil str_replace'  '' '$param_str_fil );
                 
          $param_str_fil str_replace'  '' '$param_str_fil );

                 
          // del sam emptys lines
                 
          $param_str_fil str_replace$control_chars.' '.$control_chars,  $control_chars$param_str_fil );
                 
          $param_str_fil str_replace$control_chars.$control_chars,  $control_chars$param_str_fil );
                 
          $param_str_fil str_replace$control_chars.$control_chars,  $control_chars$param_str_fil );
                 
                 
          // format { and } in my css , space
                 
          $param_str_fil str_replace$control_chars.' }',  chr(13).chr(10).'}'$param_str_fil );
                 
          $param_str_fil str_replace$control_chars.' {',  chr(13).chr(10).'{'$param_str_fil );
                 
                 
          // if in line first SPACE put $tabspce 
                 
          $param_str_fil str_replace$control_chars.' ',  $control_chars.$tabspce$param_str_fil );   
                 
                 
          // gata guta 
                 
          $param_str_fil str_replace$control_chars,  chr(13).chr(10), $param_str_fil );
                 
                 return 
          $param_str_fil
              }
              
             function 
          Replace_Callback($ar_txxt)
             {
              
          $txxt $ar_txxt[1] ; // null Ahnung 
              
          global $ar_bendrep ;
              global 
          $ix_ar_bendrep ;  // $param_not_used = '' ;
              
          for($ixx 0$ixx $ix_ar_bendrep ;  $ixx += )
              {
                
          // int preg_match ( string Suchmuster, 
                //string Zeichenkette [, array &Treffer [, int Flags [, int Versatz]]] )
                // null Ahnung:: "/^" "(.*?)" "$/s" 
                
          if(preg_match("/^".$ar_bendrep[$ixx]."(.*?)".$ar_bendrep[$ixx]."$/s",
                                        
          $txxt) )  // , $param_not_used
                  
          return $ar_bendrep[$ixx] ;
              }
             }  
          // Tipp:  Verwenden Sie strpos() oder strstr(), 
          //nicht preg_match(), wenn Sie nur überprüfen wollen, 
          //ob eine Zeichenkette in einer anderen Zeichenkette 
          //enthalten ist. Verwenden Sie dafür stattdessen die 
          //Funktionen strpos() oder strstr(), die das schneller erledigen.  
             
             
          function prg_rep_clbk($paramtxt)
             {     
                  global 
          $prc ;
                  
          // mixed preg_replace_callback ( mixed Suchmuster, 
          //callback Callback, mixed Zeichenkette [, int Limit [, int &Zähler]] )
                  
          return preg_replace_callback(
                      
          "/(".
                      
          $prc .  
                      
          "\n)/ms"// null ahnung:: "\n)/ms" 
                      
          "Replace_Callback"$paramtxt ); 
             }
             
              function 
          fil2str$param_patfilnam$param_str_fil$maxbytes 127666 // my file2string, not aray
              
          {
                  global 
          $patfilnam6;
                  
          // $fp_6=fopen($patfilnam6.'0000.css' , "r");
                  
          $fp_loc=fopen($param_patfilnam "r");
                  
          $param_str_fil fread($fp_loc$maxbytes);
                  
          fclose($fp_loc);
                  return 
          $param_str_fil;
              }
              
              function 
          str2fil$param_patfilnam$param_str_fil )
              {
                  
          // $fp_6=fopen($patfilnam6.'.css',"w");
                  
          $fp_loc fopen($param_patfilnam"w");
                  
          // fwrite($fp_loc, '/* '.$version1050.' */'.chr(13).chr(10).$str_fil6);
                  
          fwrite($fp_loc$param_str_fil );
                  
          fclose($fp_loc);
              }

              
          $str_fil6 '';
              
          $patfilnam6 'testin.txt' ;
              
          $str_fil6 fil2str$patfilnam6 $str_fil6 ) ;
              
          // my $str_fil6 = fil2str( $patfilnam6.'0000.css' , $str_fil6 ) ;
              
              // einmal     if(0) $str_fil6 = format_endofline_to_1310( $str_fil6 ) ;
                  
              
          $str_fil6 prg_rep_clbk($str_fil6); 
              if(
          $dbuggg 3)$str_fil6 format_endofline_to_1310$str_fil6 ) ;
              
              
          // result to file ..css und noch version vorne rein in ein comment
              
          $patfilnam6 'testout.txt' ;
              
          str2fil$patfilnam6,   '/* '.$version1050.' */'.chr(13).chr(10).$str_fil6 ) ;
              
          // my str2fil( $patfilnam6.'.css',   '/* '.$version1050.' */'.chr(13).chr(10).$str_fil6 ) ;
              
              // prg_rep_clbk($tststringg) is in echo 
              // for debug-echo , read result file in to array 
              // my if($dbuggg )$str_fil_ar6 = file($patfilnam6.'.css');
              
          if($dbuggg )$str_fil_ar6 file($patfilnam6);

          ?><html><head><title> repfile.php <?= $version1050 ?></title></head><body>
          <?
            if($dbuggg > 2)
              {
                echo  $e_c_h_o  , '<br /><br /><br /> 
                  tststringg: <br />' .$tststringg. '<br /><br /><br />
                  prc = <br />' .$prc. '<br /><br /><br /> 
                  prg_rep_clbk( tststringg): <br />' .prg_rep_clbk($tststringg) .'<br /><br /><br />'
                  .$patfilnam6 
                  // my .'.css : <hr>';
                  .' : <hr>';
                  
                // in Browser mit line number zeigen  
                $i=0;
                foreach( $str_fil_ar6 as $line6 )
                  {
                      printf('<span style="color:grey">%04d &nbsp; &nbsp; 
                          </span> %s ', ++$i, ' ' ); highlight_string( $line6 ) ;
                  }
              }
                
              if($dbuggg )echo "<hr> dbuggg = $dbuggg <br />";
              
          ?></body></html>
          Zuletzt geändert von andijota; 06.06.2006, 08:53.

          Kommentar


          • #6
            testin.txt ::

            PHP-Code:
                if(0){ // testin.txt :: ///////////////////////////////////////////
                ///////////////////////////////////////////////////////////////////
                
                
            $todo ;  

                
            /**
                 *
                   DEL this
                 *
                 */
                
                
            function dummy(){ ; } ;
                
                
            // and del that
                // prg_rep_clbk($tststringg) is in echo //  
                // for debug-echo , read result file in to array 
                
                
            if($dbuggg )$str_fil_ar6 file($patfilnam6.'.css');
                
                if(
            0) echo ' end ' 
                
                
            /////////////////////////////////////////////////////////////////////
                
            // /end testin.txt :: ///////////////////////////////////////////// 


            echo:
            PHP-Code:

            echo teststring :
            ------ 
            _beg_pattrn1_ one one one _end_pattrn1_ pause §§ and 4 multi §§
            _FULL_pattrn2_ dont replace two 
            /* two */ two pause2 // comment2 // 2.2
            %%3 three three three 3%% pause3
            §§4 
            and for last 4§§ ------



            prc =
            _beg_pattrn1_.*?_end_pattrn1_|_FULL_pattrn2_|%%3.*?3%%|§§.*?§§|\/\*.*?\*\/|\/\/.*? |\/\/.*? |\/\/.*? |



            prg_rep_clbktesttxtt):
            ------ 
            _repl1_ pause _repl4_
            _repl2_ dont replace two two pause2
            _repl3_ pause3
            _repl4_ 
            ------



            testout.txt 
            ------
            0001     /*  version: 1061
            0002              */
            0003    
            0004       $todo 

            0005       
            0006       
            function dummy(){ ; } ;
            0007       
            0008       
            0009       
            if($dbuggg )$str_fil_ar6 file($patfilnam6.'.css');
            0010       if(0) echo ' end ' ;  

            ------
            dbuggg 
            Zuletzt geändert von andijota; 06.06.2006, 09:17.

            Kommentar

            Lädt...
            X