Hi guys

I need your help 🙂 I have an array with a list of common acronyms as keys, and the unabbreviated equivalents as the values, e.g.

$Acronyms = array(
  "P4" => "Pentium 4",
  "PC" => "Personal Computer"
  ...
);

The idea is to pass a piece of text to a simple function, and to use str_replace() to replace any matching acronyms with some HTML code (e.g. PC would become <acronym title="Personal Computer">PC</acronym>).

I have a very simple function to do this, but the only problem is I cannot seem to replace the matching values with anything other than the array value itself, e.g.

[b]// Works ...[/b]
function Acronyms ($String)
{
  return str_replace(array_keys($Acronyms), array_values($Acronyms), $String);
}

[b]// Does not work ...[/b]
function Acronyms ($String)
{
  return str_replace(array_keys($Acronyms), '<acronym title="'.array_values($Acronyms).'">'.array_keys($Acronyms).'</acronym>', $String);
}

Is there any way I can solve this problem short of ...

  1. Adding the HTML code to every value in the acronyms array

  2. Using preg_replace() or something similarly over the top

Any help or suggestions would be greatly appreciated.

Thank you in advance

Jonty

P.S. Am I likely to place much strain on the server if the acronyms array contains ~100 keys, and function is called to parse, say, ~5kb of text every time the page is hit?

    A quick glance looks to me like it could be all the 's, "s and things like that are causing the problem. A newbie's suggestion would be to add:

    '<acronym title="'.array_values($Acronyms).'">'.array_keys($Acronyms).'</acronym>'

    To a variable then use that in your script, like so:

    $variable = '<acronym title="'.array_values($Acronyms).'">'.array_keys($Acronyms).'</acronym>';

    then use:

    return str_replace(array_keys($Acronyms), $variable, $String);

    Very poor suggestion I know, but at the very least, it might make something else occur to you 🙂

    Shuriken1.

      What's happening is that when you use '<acronym title="'.array_keys($Acronyms)."........ and what not, you're no longer using an array as the replacement argument, but a string.

      To fix this, try this:

      $replacements = array();
      foreach($Acronyms as $row) {
         $replacements[] = '<acronym title="'.key($row).'">'.$row.'</acronym>';
      }
      

      Then use $replacements as the second argument of str_replace. This is the simplest way to effectively solve this problem.

      Another suggestion, and you may or may not run into this problem depending on what you're using this code for, is to place the acronyms in some sort of bracket in order for them to be replaced. For example, using "[PC]"=>"Personal Computer" or "[OS]"=>"Operating System". This way, UPC doesn't automatically get replaced with "U<acronym title="...">PC</acronym>" and BIOS doesn't get replaced with "BI<acronym title="...">OS</acronym>"

        By the way, if you want to test the time it takes to generate a page, use this simple code:

        <?
        function getmicrotime(){ 
            list($usec, $sec) = explode(" ",microtime()); 
            return ((float)$usec + (float)$sec); 
        }
        $time_start = getmicrotime();
        
        .... Insert normal code here ....
        
        
        $time_end = getmicrotime();
        $time_total = $time_end - $time_start;
        echo "Page generated in $time_total seconds.";
        ?>
        

          Thanks python_q, that's perfect 😃

          Thank you!

            Write a Reply...