943,754 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 4611
  • PHP RSS
Feb 9th, 2009
0

Split a string into two parts?

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
sarithak is offline Offline
183 posts
since Aug 2008
Feb 9th, 2009
0

Re: Split a string into two parts?

Simply use the explod function to convert the string into an array. So for your example, the following code would apply:
php Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Feb 9th, 2009
0

Re: Split a string into two parts?

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........
Reputation Points: 10
Solved Threads: 7
Junior Poster
sarithak is offline Offline
183 posts
since Aug 2008
Feb 9th, 2009
0

Re: Split a string into two parts?

Try the following to match the 2:
php Syntax (Toggle Plain Text)
  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. }
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Feb 9th, 2009
0

Re: Split a string into two parts?

Oh, just to throw this in-MySql has a built-in search "help"

Quote ...
<?
$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.
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
Andrieux is offline Offline
60 posts
since Jan 2009
Feb 10th, 2009
0

Re: Split a string into two parts?

Hi frnds....

thanks for all of u...

i think the above all answers are working fine...
Reputation Points: 10
Solved Threads: 7
Junior Poster
sarithak is offline Offline
183 posts
since Aug 2008
May 30th, 2009
0

Re: Split a string into two parts?

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
php Syntax (Toggle Plain Text)
  1. $mysqldata = 'Blue Bird';
  2. $mysqldata = str_replace (" ", "", $mysqldata);
  3. echo $mysqldata;

would output BlueBird

and to make it lowercase

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 4
Junior Poster
Merlin33069 is offline Offline
126 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Php insert to html
Next Thread in PHP Forum Timeline: draw image with bold/italic text





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC