Reply
help...problem with a bug in my code...
Old 07-15-2004, 10:12 PM help...problem with a bug in my code...
johanesw's Avatar
Extreme Talker

Posts: 174
Location: If I told you that, I'm afraid I'd have to kill you!
Hello guys I'm making a search engine script. There is one part in the script that I have problem with...

This function is supposed to make the description that will be displayed bolded if it's the same with the search term....

PHP Code:
function make_bolded($string,$term){
    
$words explode(" ",$string);
    
$bolded_string "";
    
$sterms explode(" ",$term);
    for(
$i=0;$i<count($words);$i++){
        
$w $words[$i];
        
$lowword strtolower($w);
        for(
$x=0;$x<count($sterms);$x++){
            
$lowsterm strtolower($sterms[$x]);
            switch(
$lowword){
                case 
$lowsterm $w "<b>" $words[$i] . "</b>";
                break;
                case 
"{$lowsterm}'" $w "<b>" $words[$i] . "</b>";
                break;
                case 
"{$lowsterm}'s" $w "<b>" $words[$i] . "</b>";
                break;
                case 
"{$lowsterm}," $w "<b>" $words[$i] . "</b>";
                break;
                case 
"{$lowsterm}." $w "<b>" $words[$i] . "</b>";
                break;
                case 
"{$lowsterm};" $w "<b>" $words[$i] . "</b>";
                break;
            }
        }
        
$bolded_string .= " " $w " ";
    }
    return 
$bolded_string;

for example...if you do $boldedhello = make_bolded("hello my","hello") , this function's supposed to make the "hello" in the string "hello my" bolded...so $boldedhello should contain the string "<b>hello</b> my"....

The problem is...that it only change the words into bolded if it's not the first or last word in the string...and I wonder what's wrong with my code that it does such thing.

For example

$bolded = make_bolded("what the heck","what");

will make $bolded contain "what the heck" even though it's supposed to be "<b>what</b> the heck"...

But if you do

$bolded = make_bolded("what what the heck","what");

It will change the second what into bolded like this:

"what <b>what</b> the heck"....

Can any of you help...thank you very much.....
johanesw is offline
Reply With Quote
View Public Profile Visit johanesw's homepage!
 
When You Register, These Ads Go Away!
Old 07-15-2004, 11:07 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
PHP and Unicode with UTF-8
Posts: 3,111
Location: Toronto, Ontario
Seems to be working for me. I just cleaned it up a bit (don't think I did anything else lol):

PHP Code:
function make_bolded($string,$term){
    
$words explode(' '$string);
    
$bolded_string '';
    
$sterms explode(' '$term);

    for(
$i 0$i count($words); $i++)
    {
        
$w $words[$i];
        
$lowword strtolower($w);

        for(
$x 0$x count($sterms); $x++)
        {
            
$lowsterm strtolower($sterms[$x]);

            switch(
$lowword)
            {
                case 
$lowsterm:
                case 
"$lowsterm'":
                case 
"$lowsterm's":
                case 
"$lowsterm,":
                case 
"$lowsterm.":
                case 
"$lowsterm;":
                    
$w '<b>' $words[$i] . '</b>';
                    break;
            }
        }

        
$bolded_string .= " " $w " ";
    }

    return 
$bolded_string;
}

echo 
make_bolded('hello; there''hello'); 
Produced:
hello; there
(<b>hello;</b> there)
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 07-16-2004, 01:30 AM
johanesw's Avatar
Extreme Talker

Posts: 174
Location: If I told you that, I'm afraid I'd have to kill you!
hmmm...you're right...there's nothing wrong with this function...I tested the function and it worked perfectly...

Arrrgh...then the problem's gotta be in other part of my script...errr...i'll need a good luck searching for the problem

anyway, thanks for cleaning up the code....It's easier to decipher (I guess my scripts're too crammed up...)

PS: I hate bugs...especially when I have 238 lines of code....

Last edited by johanesw : 07-16-2004 at 01:36 AM.
johanesw is offline
Reply With Quote
View Public Profile Visit johanesw's homepage!
 
Reply     « Reply to help...problem with a bug in my code...
 

Thread Tools

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 Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13051 seconds with 12 queries