Hi,

Im trying to make a script that reads $string_of_words and replaces all the accronyms (specified in the $accronyms_list) with somthing like..
<accronym name="hypertext markup language">HTML</accronym>

this was my attempt that failed misaribly...

 
<?

$string_of_words = 'this is a string that contains accronyms like php , html and  xml';

$accronym_list = array('html', 'php', 'xml');

$accronym_defs = array('HyperText Markup Language',
                                        'php hypertext preprocessor',
                                        'extensible Markup language'
                                          );

for($i=0;$i<=(count($accronym_list)-1);$i++){

$new_string = str_replace(
                             $accronym_list[$i], '
       <accronym name="'.$accronym_defs[$i].'">'.    $accronym_list[$i].'</accronym>',$string_of_words);

}
echo $new_string;
?>
 

Im Really stuck on this hope it makes sense... plz help :S

    you do not need two separate arrays for this, just use one associative array.

    <?php
    $accronyms = array(
    'HTML' => 'Hypertext Markup Language',
    'PHP' => 'PHP Hypertext Preprocessor',
    'XML' => 'Extensible Markup language'
    );
    
    foreach ($accronyms as $key => $value)
    {
    	$accronyms[$key] = '<accronym name="' . $value . '">' . $key . '</accronym>';
    }
    
    $string = 'this is a string that contains accronyms like PHP, HTML and XML';
    echo str_replace(array_keys($accronyms), $accronyms, $string);
    ?>
    
      devinemke wrote:
      <?php
      $accronyms = array(
      'HTML' => 'Hypertext Markup Language',
      'PHP' => 'PHP Hypertext Preprocessor',
      'XML' => 'Extensible Markup language'
      );
      
      $string = 'this is a string that contains accronyms like PHP, HTML and XML';
      
      //--------------------------------------------------------------------------------
      echo str_replace(array_keys($accronyms), $accronyms, $string);
      //--------------------------------------------------------------------------------
      // That's hot.  This should help me out in the future.
      // Thanks for the vicarious help.
      ?>
      

      ..

        devin, just one little tweak, if you will permit me: (use the TITLE attribute in the acronym tag)

        <?php
        // list of accronyms
        $acronyms = array(
        	'HTML' => 'Hypertext Markup Language',
        	'PHP' => 'PHP Hypertext Preprocessor',
        	'XML' => 'Extensible Markup language',
        	);
        
        foreach ($acronyms as $key => $value){
        	$acronyms[$key] = '<acronym title="' . $value . '">' . $key . '</acronym>';
        }
        
        $string = 'this is a string that contains accronyms like PHP, HTML and XML';
        echo str_replace(array_keys($acronyms), $acronyms, $string);
        ?> 

        PS. You totally rock, devinemke. I copied and pasted this in my snippets saved file 🙂

          Write a Reply...