Current location: Hot Scripts Forums » Programming Languages » PHP » Help with using preg_replace


Help with using preg_replace

Reply
  #1 (permalink)  
Old 06-23-09, 04:57 PM
myklt myklt is offline
Newbie Coder
 
Join Date: Feb 2009
Location: UK
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Help with using preg_replace

I am having problems with finishing a function I have made which uses PHP's preg_replace.

The function runs through text within a forum I am designing to highlight target words to the user based on their interests etc I am hoping to allow a customizable tool-tip to pop-up over the highlighted areas to give the user extra information/ links etc.

The function is highlighting the words 100% and my tooltip is displaying the way I want it atm BUT the problem I am having is getting the PHP variable $definition to output its content instead of "$definition".

The problem seems to be the method I am trying to output a variable within a String. I have exausted every way I know of doing this and would like help. I have tried using <?php echo "$definition"?>, ".$definition.", print $definition, echo $definition....

Does anyone know any way or method to get the output of $definition to populate the tooltips?

PHP Code:

function getTargetWords($fContent){

....
.... 
....

/***Definition***/
       
$definition "Definition text for inside the tooltip";

/***!!!!!!!!To be fixed to output tooltip definition not "$definition" !!!!!!!!!***/        
       
$replace '<a href="#"><span class="highlight_word">\1<span class="tooltip">$definition</span></span></a>';
                
 
/*** highlight the words ***/
         
$fContent preg_replace("/\b($word)\b/i"$replace$fContent);
....
....
....

any help will be appreciated, thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-24-09, 10:05 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Try using double quotes:
PHP Code:

       $replace "<a href=\"#\"><span class=\"highlight_word\">\1<span class=\"tooltip\">$definition</span></span></a>"
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-25-09, 08:02 AM
myklt myklt is offline
Newbie Coder
 
Join Date: Feb 2009
Location: UK
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!! didn't think about trying double quotes...

Have just tried them and im getting the tooltip deinition now (will have to loop the values out to get them to work properly which im sure I can do) but ive lost the name of the highlighed word which is now displaying a (0001) in a square box. any suggestions?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-25-09, 04:12 PM
myklt myklt is offline
Newbie Coder
 
Join Date: Feb 2009
Location: UK
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Ive managed to loop the definition values into the tooltip ok but still can't get the original words that were targeted to display properly.

example:
if i was searching for the word "PHP" or "CSS" in a paragraph of text "$fcontent" the words "PHP" and "CSS" thoughout the text would be replaced by a square with 00 on the top line and 01 on the bottom. The tooltip works though and displays the appropriate definition for "PHP" or "CSS" for every word that was "PHP" or "CSS".

any suggestions?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-25-09, 07:26 PM
myklt myklt is offline
Newbie Coder
 
Join Date: Feb 2009
Location: UK
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
I have built a small demo of my problem to visually show what the issue is, I believe its quotations again thats the problem.

Code:
<?php
$source = "My target word is PHP";
$target = "PHP";
$definition = "PHP is a rite pain getting!";
$pattern = "/\b($target)\b/i";
$replacement = "<a href=\"#\"><span class=\"highlight_target\">\1<span class=\"tooltip\">$definition</span></span></a>";
echo preg_replace($pattern, $replacement, $source);
?> 

<head>
<style type="text/css">
/* css tooltip
================================== */

span.highlight_target {
  position: relative; 
  background-color: yellow;
  border: 1px dashed #000000;
}
 
span.highlight_target span.tooltip {
  display: none;
}

/* tooltip will display on :hover event */
 
span.highlight_target:hover span.tooltip {
  display: block;
  z-index: 100;
  position: absolute;
  width: 200px;
  padding: 3px 6px;
  border: 1px dashed #000000;
  background-color: #f7f7ee;
  text-align: center;
}
</style>

</head>

<body>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 02:41 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.