Go Back   HTMLCenter Web Development Forums > Web Design and Development > Programming and Scripting

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 06-23-2008, 04:14 PM
Registered User
 
Join Date: Jul 2004
Posts: 224
preg_replace problem

WHen it comes to highlighting I use the preg_replace. But I'm getting the problem that the tags wrap around every single letter rather than the word intended to highlight:
PHP Code:
$strText $row['text_data'];
$strText preg_replace("/(".$newSearchTheseArr[$j].")/i""<span class='' id='' style='color:blue; font-weight:bold;'>$1</span>"$strText);
echo 
$strText."<br />\n"
Reply With Quote

  #2 (permalink)  
Old 06-23-2008, 09:09 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,399
Can you show us an example of what value might be held in $newSearchTheseArr[$j]? I have a feeling you'd be better off using str_replace instead of preg_replace, since it appears you're searching the text for a specific string.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #3 (permalink)  
Old 06-24-2008, 11:26 AM
Registered User
 
Join Date: Jul 2004
Posts: 224
I was reworking on the scripting.

I think I know what it's doing though I did these changes. I put these outside the While loop:
PHP Code:
function highlight $str$words )
        {
            if ( 
is_array$words ) ) {
                
$words array_map'preg_quote'$words );
                
$words join$words'|' );
            } else {
                
$words preg_quote( (string)$words );
            }
            
            
$re '<span style="color:blue;font-weight:bold;">$1</span>';
            return 
preg_replace'/\b(' $words ')\b/i'$re$str );
        }
        function 
thewords(){
            for (
$j=0$j count($newSearchTheseArr); $j++){
                echo 
$newSearchTheseArr[$j].", ";
            }
        } 
I created thewords function to set the array of words (does it work this way?)

Then:
PHP Code:
while($row mysql_fetch_array($result)){
        echo 
"<span id='regular[]' style='padding: 5px;'>";
        echo 
"<span style='font-weight: bold;'>".$row["book_title"]." ".$row["chapter"].":".$row["verse"]." ";
        echo 
"</span>\n";    

        
$words = array(thewords());
        
$str   $row['text_data'];
        
        
var_dumphighlight$str$words ) );
        echo 
"<br />\n";

I get the words outside the span tags:
Code:
shall<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>be
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>called
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>Woman
<span style="color: blue; font-weight: bold;"></span>, 
<span style="color: blue; font-weight: bold;"></span>because
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>she
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>was
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>taken
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>out
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>of
<span style="color: blue; font-weight: bold;"></span> 
<span style="color: blue; font-weight: bold;"></span>Man
<span style="color: blue; font-weight: bold;"></span>."
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Image Problem trasper Graphics and Flash 5 02-01-2008 10:43 AM
framespacing="0" problem solved cssiscool Programming and Scripting 4 06-14-2007 07:27 AM
javascript / php / forms problem divx Programming and Scripting 2 05-24-2007 11:13 AM
weird problem with FF 1.5.0.6 flashwood59 Programming and Scripting 0 09-11-2006 01:35 PM


All times are GMT -5. The time now is 04:34 AM.

 
KickApps
Clicky Web Analytics


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter