Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP issue

  • 09-11-2005 05:41PM
    #1
    Registered Users, Registered Users 2 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