Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP issue

Options
  • 09-11-2005 5:41pm
    #1
    Registered Users Posts: 648 ✭✭✭


    Hi,

    Im doing a site where i have a db of terms and definitions and on the site i want to replace those terms with s popup with the definition.
    This works fine - here is the code :
    
    $query = "SELECT term, definition FROM #__glossary";
            $database->setQuery($query);
            $results = $database->loadObjectList();
    	 foreach($results as $result){
    		$link=' <a class="mosinfopop" href="javascript:void(0)" onmouseover="return overlib(\'' .$result->definition. '\', CAPTION, \'' . $result->term . '\');" onmouseout="return nd();" style=\"$css\">'.$result->term.'</a> ';
    				 $text=preg_replace('/\s+'.$result->term.'\s+/',$link,$text);
    				// $text = str_replace(strtolower($result->term),$link,$text);
    		 }
    


    HOWEVER problems arise when a term is found within one of the already inserted popup (from a previous loop!)


    Anyone know how i would tackle this please ?


Comments

  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    preg_replace needs the following arguments:
    an array of patterns to match
    an array of replacements
    and the text within which to do the replacements.

    Looking at your loop, your doing the replacement inside the loop instead of outside, that's got to be hitting performance for starters.

    Within the loop you need to build the array of patterns $terms, each entry made out of '/\s+'.$result->term.'\s+/'
    also the array of replacements (the anchor link currently assigned to $link)

    After the loop:
    preg_replace($terms,$links,$text);

    Now depending on how preg_replace iterates through the array, it may still have the problem because it might simply be the equivalent of a sequence of str_replace statements which I see you've hit already.

    Eg you replace the term 'Chavez' with a popup link including the caption 'President of Venezuela, a latin American country with a population..'
    Next term however is 'Venezuela'', and it lands right in the middle of the previous popup caption.

    If so, here's one of my over-complicated suggestion specials based on ignorance of the simpler way that must exist:

    When Chavex gets replaced with a link, say you put five @ symbols before and after.
    @ appended and prepended.

    This means you need to modify the pattern matching terms to exclude matches within those @ delimeters.
    So once a replacement is made, no other pattern will match within it.

    When the match is done, just do a str_replace to get rid of the @symbols.

    If I'd time I'd code it up out of fascination, but check out http://uk.php.net/manual/en/reference.pcre.pattern.syntax.php if you need to refer to the regexp syntax.


Advertisement