SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Slow page download & entity replace function

    Hi folks:

    I'm tweaking a site comprising a lot of user inputted data with the inevitable array of character types/sources that go hand-in-hand with user input.

    I came across a function which does a great job of transforming symbols to their equivalant SGML entities to the extent that the site now has XHTML 1.0 compliance.

    However - the code that does this now adds upwards of 5-10 seconds to page download/rendering time (specifically the homepage) and I'd love to know what others have done/are doing to translate symbols from a range of apps (MS Word etc) into SGML compliant entities.

    * The site in question is: www.featuresexec.com
    * The code I use is as follows:

    PHP Function to replace symbols with SGML entities:
    PHP Code:
    function entity_replace($string)
    {
     
    $trans_array = array();
     for(
    $i=127;$i<255;$i++)
     {
    // W3C validator doesn't like ¸ for replacing Euro symbol:  
    if(preg_match("##",$string))
      {
       
    $trans_array[chr($i)] = "€";
      }
    // It doesn't like the suggestion for replacing MS Word single quotes:  
      
    elseif(preg_match("##",$string))
      {
       
    $trans_array[chr($i)] = "&apos;";
      }
      else
      {
       
    $trans_array[chr($i)] = "&#{$i};";
      }
     }
     
    // htmlentities() only converts up to &#127 - so do the rest:
     
    $fixed htmlentities($string,ENT_QUOTES);
     return 
    strtr($fixed,$trans_array);
     return 
    $string;

    The function entity_replace() in action:
    PHP Code:
    $t->Set_block("body","list-non-press-events","listnonpressevents");
     while(
    $array DBFetchArray($query_npe))
     {
      
    $t->set_var("npe_id",imangle($array['ID']));
      
    $t->set_var("npe_title_brief",entity_replace(abbr($array['Name'],19)));
      
    $t->set_var("npe_title_full",entity_replace($array['Name']));
      
    $t->parse("listnonpressevents","list-non-press-events",true);
     } 
    As entity_replace() is used within other loops I think it's slowing everthing down. If I comment out the function, page download is super-fast at about 1.5/2 sec. Can anyone suggest a faster way of achieving the same thing?
    Many thanks

  2. #2
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here we go:
    PHP Code:
        $chars = array();
        for (
    $i 127$i 255$i++) $chars[chr($i)] = "&#{$i};";
        
    $chars[""] = "&euro;";
        
    $chars[""] = "&apos;";
        function 
    entity_replace($string) {
            global 
    $chars;
            
    $fixed htmlentities($string,ENT_QUOTES);
            return 
    strtr($fixed,$chars);
        } 
    The script above will reduce the loading times quite a lot

  3. #3
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sorccu
    The script above will reduce the loading times quite a lot
    Wow! I still have XHTML compliancy and the page loads in a second or two. Many, many thanks!



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
  •