Current location: Hot Scripts Forums » Programming Languages » PHP » need help for php compare string


need help for php compare string

Reply
  #1 (permalink)  
Old 07-22-10, 07:38 PM
tharsoe tharsoe is offline
Newbie Coder
 
Join Date: Apr 2008
Location: Singapore
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
need help for php compare string

Hi everybody I need your help.

This is my code.

PHP Code:

function music($str1,$str)

            {
                
$array explode(",",$str);
                
//print_r($array);
                
foreach($array as $k=>$v)
                {
                    if(
$v == 'Jazz / Blues') echo 'love';
                    if(
$str1 == $v) echo 'hooya';
                    
                    if(
ereg_replace("/","-",$v) == ereg_replace("/","-",$str1)) echo 'yahoo';
//                    if(substr_compare($v,$val,0) == 0) return 'checked="checked"';
                    //if(strcmp($v,$val) == 0) return 'checked="checked"';
                
}
            }
            
music("Jazz / Blues",$_SESSION['mem_step3']['MUSIC_PREFERENCES']); 
in $_SESSION['mem_step3']['MUSIC_PREFERENCES'] there are a string like this "Classical, Jazz / Blues, R&B / Soul"

but there are not any output. pls help me. what am I wrong? waiting your advice.


Thank
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 07-23-10, 01:17 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If you look at this string, "Classical, Jazz / Blues, R&B / Soul".
You will see that there is a space before Jazz / Blues and R&B / Soul.
But in your use of "Jazz / Blues", you will see that there is no space before (" Jazz / Blues").

You can easily solve your problem by adding a space or use trim().
PHP Code:

<?php
session_start
();
$_SESSION['mem_step3']['MUSIC_PREFERENCES'] = "Classical, Jazz / Blues, R&B / Soul";
function 
music($str1,$str)
            {
                
$array explode(",",$str);
                
//print_r($array);
                
foreach($array as $v)
                {
                    if(
trim($v) == 'Jazz / Blues') echo 'love';
                    if(
$str1 == trim($v)) echo 'hooya';

                    if(
ereg_replace("/","-",trim($v)) == ereg_replace("/","-",$str1)) echo 'yahoo';
//                    if(substr_compare($v,$val,0) == 0) return 'checked="checked"';
                    //if(strcmp($v,$val) == 0) return 'checked="checked"';
                
}
            }
            
music("Jazz / Blues",$_SESSION['mem_step3']['MUSIC_PREFERENCES']);
?>
__________________
Jerry Broughton

Last edited by job0107; 07-23-10 at 01:24 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
mysamsung (07-23-10)
  #3 (permalink)  
Old 07-23-10, 04:23 AM
tharsoe tharsoe is offline
Newbie Coder
 
Join Date: Apr 2008
Location: Singapore
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Thanks you job0107. Thx alot.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to tharsoe For This Useful Post:
mysamsung (07-23-10)
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP C-Sharp DB Access Wrapper Question digioz ASP.NET 1 09-08-08 10:54 AM
search function using dot net nishudude_13 Windows .NET Programming 1 08-28-08 12:28 PM
[SOLVED] searching through a grid view painthu ASP.NET 5 05-21-08 11:11 AM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM


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