Location: PHPKode > scripts > html_special_chars_encode > htmlspecialcharsencode/index.php
#!/usr/local/bin/php4 -q
<?php

/*
    html_special_chars_encode Version 1.0.0

    Written By Jeremy Brand <hide@address.com>
    http://www.nirvani.net/software/
    http://www.jeremybrand.com/Jeremy/Brand/Jeremy_Brand.html

    PURPOSE: to encode STDIN to be included in html where result is written to STDOUT.  
             It replaces all occurances of characters known to break HTML.

    1) Preferable name you should save this file as "html_special_chars_encode"
    2) save it in /usr/local/bin/ (or somewhere else in your PATH).
    3) This file should have the "execute" bit set.  
       (chmod a+x /usr/local/bin/html_special_chars_encode)

    I release this code under the public domain.

    TYPICAL USAGE:
    --------------
    (from the UNIX prompt) 
      cat file_to_be_encoded.txt | html_special_chars_encode > encoded_output_file.txt

     -- OR --

      html_special_chars_encode < file_to_be_encoded.txt > encoded_output_file.txt

    NOTE:
    -----
      Example file encoded_output_file.txt can now be safely included in HTML, 
      commonly between <pre></pre> tags.

    EXAMPLE INPUT (unsafe for HTML):
    --------------------------------
      "Hello, World"   <hide@address.com>

    EXAMPLE OUTPUT (safe for HTML):
    -------------------------------
      &quot;Hello, World&quot;   &lt;hide@address.com&gt;
*/

      $file = 'php://stdin';
      //$file = '/dev/stdin';    # If php://stdin doesn't work, use this instead.

      $fd = fopen($file, 'r');
      $buf = (string)NULL;

      if ($fd)
      {
        while(!feof($fd))
          $buf .= fread($fd, 1024);
        fclose($fd);
        print htmlspecialchars($buf);
      }
      
?>
Return current item: html_special_chars_encode