Hello all. Is there a way to read an HTML file on my server, strip out just a certain part, and keep the rest?

I know that sub_str would do the trick normally, but my problem is that each page I am editing is just slightly different.

I am trying to strip out all of the javascript contained on a page, but the actualy script is a little different each page.

They do follow the same start and end script tags though, just the content between the tags is different.

Thanks!

    Don't even have to get into regex.

    $fh = fopen('filename.html');
    $file = fread($fh,filesize('filename.html');
    fclose($fh);
    
    $start = strpos($file,'<script');
    $end = strpos($file,'</script>',$start) + 9;
    $display = substr($file,0,$start) . substr($file,$end);
    $file = '';
    echo $display;
    

      Thanks for your input to the bpth of you.

      I tried your method drawmack, but it seemed to take my source and wipe it all and add the last few letters of the file name and the extension of the file, and then wrote that to the file.

      Here is what I am working with. Obviously, it doesnt work. Maybe by showing you what I am working with, I might be able to shed a little more light on the problem.

      $open = opendir($onsite_dir);
      while (($check = readdir($open))!= false){ 
      if ($check != "." && $check != "..") { 
      $handle = fopen($check, "r");
      if (!function_exists('file_get_contents')) {
       $read = fread($handle, 800000);
      	} else {
       $read = file_get_contents($onsite_url.$check);
      			}
      str_replace("<script", "<?php /* <script", $read);
      str_replace("</script>", "</script> */ ?>", $read); 
      
      if(!chmod($onsite_dir.$check, 0755)){ echo "Cannot change permission on file."; exit;}
      if(!$open = fopen($onsite_dir.$check, "w")) { echo "Error :: Cannot open file: ".$check; exit;}		
      if (!fwrite($open, $read)) { echo "Cannot write to file: ".$handle."<br>"; exit;}
      echo $check." Success.";	
      } 
      }
      

      With the script above it doesnt do anything at all. Here is what all of the java tags look like.

      <script type="text/javascript" language='javascript'>
      //bunc of crap here
      </script>

      Thanks for your help thus far!

        Try this one,

        $read = str_replace("<script", "<?php /* <script", $read); 
        $read = str_replace("</script>", "</script> */ ?>", $read); 
        

        Thomas

          or try

          $read = preg_replace("/(<script.*?</script>)/im","<?PHP /*\\\\1*/ ?>",$read);
          

          remove that whitespace after </script> PHPBuilder messes up the code a little bit.

          Thomas

            tsinka, drawmack, TheDefender, Thanks for your input and help.

            tsinka, the following worked:

            $read = str_replace("<script", "<?php / <script", $read);
            $read = str_replace("</script>", "</script>
            / ?>", $read);

            Thanks!

              Write a Reply...