Split a string into two parts?

Thread Solved

Join Date: Aug 2008
Posts: 159
Reputation: sarithak is an unknown quantity at this point 
Solved Threads: 6
sarithak sarithak is offline Offline
Junior Poster

Split a string into two parts?

 
0
  #1
Feb 9th, 2009
Hi frnds..

i want to split a string into two parts..

example:

$string="hyderabad,india";

i want to divide this based on , ...

$string1= "hyderabad";
$string2="india";


plz help me asap...how can i get the output above...
My best wishes ... from my soul ... for everyone!
Keep Smiling....Never Depress
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,527
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 137
cwarn23's Avatar
cwarn23 cwarn23 is online now Online
Posting Virtuoso

Re: Split a string into two parts?

 
0
  #2
Feb 9th, 2009
Simply use the explod function to convert the string into an array. So for your example, the following code would apply:
  1. $string="hyderabad,india";
  2. $strings=explode(',',$string);
  3. echo $strings[0]; // outputs: hyderabad
  4. echo "<br>"; // new line
  5. echo $strings[1]; // outputs: india
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - Oopy Doopy Do 2U2!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 159
Reputation: sarithak is an unknown quantity at this point 
Solved Threads: 6
sarithak sarithak is offline Offline
Junior Poster

Re: Split a string into two parts?

 
0
  #3
Feb 9th, 2009
Thank u ...

i have been cleared my doubt by using explode function...
but i have another doubt here...

For searching....
when i comparing my input string with database string, i phase problem with case sensitive and white spaces....
plz give me a suggetion to do this....

ex:

input = "bluefox";

but in my database that word is like "Blue Fox"..........

i need to get results without changing database strings....

Thanks & regards
Saritha........
My best wishes ... from my soul ... for everyone!
Keep Smiling....Never Depress
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,527
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 137
cwarn23's Avatar
cwarn23 cwarn23 is online now Online
Posting Virtuoso

Re: Split a string into two parts?

 
0
  #4
Feb 9th, 2009
Try the following to match the 2:
  1. $input='bluefox';
  2. $database_input='Blue Fox';
  3.  
  4. if (str_replace(' ','',strtolower($input))==str_replace(' ','',strtolower($database_input)))
  5. {
  6. echo "They match";
  7. }
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - Oopy Doopy Do 2U2!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 60
Reputation: Andrieux is an unknown quantity at this point 
Solved Threads: 4
Andrieux Andrieux is offline Offline
Junior Poster in Training

Re: Split a string into two parts?

 
0
  #5
Feb 9th, 2009
Oh, just to throw this in-MySql has a built-in search "help"

<?
$input = 'blue fox';

$query = mysql_query("SELECT * FROM `table`WHERE `column` LIKE '%$input%'");

while($show = mysql_fetch_assoc($query)) {
echo $show['column'] . "<br .";
}

?>
Of course, you'd want to check if there is anything returned. use mysql_num_rows(); for that. If the value of that equals != 0, then you're good. else { return an error message }.
Last edited by Andrieux; Feb 9th, 2009 at 6:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 159
Reputation: sarithak is an unknown quantity at this point 
Solved Threads: 6
sarithak sarithak is offline Offline
Junior Poster

Re: Split a string into two parts?

 
0
  #6
Feb 10th, 2009
Hi frnds....

thanks for all of u...

i think the above all answers are working fine...
My best wishes ... from my soul ... for everyone!
Keep Smiling....Never Depress
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 113
Reputation: Merlin33069 is an unknown quantity at this point 
Solved Threads: 4
Merlin33069 Merlin33069 is offline Offline
Junior Poster

Re: Split a string into two parts?

 
0
  #7
May 30th, 2009
you could also take the input from the mysql as a string, trim out the space in between the words and set it as lowercase letters:

to remove spaces
  1. $mysqldata = 'Blue Bird';
  2. $mysqldata = str_replace (" ", "", $mysqldata);
  3. echo $mysqldata;

would output BlueBird

and to make it lowercase

  1. $mysqldata = strtolower($mysqldata);
  2. echo $mysqldata;

would now return bluebird

and the strings would now match
Last edited by peter_budo; May 30th, 2009 at 5:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Do you geek alone?

<<TimmCo>> Custom Computers

~Executive
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC