LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2009, 07:21 AM   #1
Yoyo302
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 36

Rep: Reputation: 15
problem with comparing strings in php


Hi,
I have a problem while comparing strings in php.
One of them is string received from POST method and another one is element of the table which was read from a file.

the code is:
Code:
$data=file($_POST[a])
if( $data[1] === $_POST[b] )
{
  echo 'fine'.<br>;
}
else
{
  echo 'bad'.<br>;
}
both strings are same when i try to echo them, but i tried many different ways of comparing them, including strcmp() and converting them to some other types, but none of them worked properly...

I will be grateful for any help
 
Old 06-28-2009, 08:27 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Do either of your string have extra whitespace on either end of the text, (Strings read from a file often have a new line at the end)?

Check by echo with quotes around the strings.
If so look up the trim() function.
 
Old 06-28-2009, 09:30 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
The file function puts a newline at the end of each array element. The POST array contains strings which do not have that newline. Can that be the problem?

String matching can be a problem, spaces, newlines, capitalization are common pitfalls. Unless you have a special reason to make verbatim comparisons, it is better to compare parts of a string from which you are sure they'll not ever change.

jlinkels
 
Old 06-28-2009, 12:48 PM   #4
Yoyo302
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 36

Original Poster
Rep: Reputation: 15
Thank you for help :]
the problem was in that i tried to connect 2 strings by "+", like $string1+$string2, as it can be done in other languages, i found out that it is done by "." in php...

i added "\n" at the end of POST string and it works fine
 
Old 06-28-2009, 08:42 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I would recommend removing excess white space (see trim() or rtrim() to just remove trailing white space) rather than adding a new line to one of yoyr test strings. The reason for this is that if the last line on your file doesn't end with a white space you will still have a problem.

A second reason is that different OS have different line endings.
 
Old 06-30-2009, 04:16 AM   #6
Yoyo302
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 36

Original Poster
Rep: Reputation: 15
that's right, i will consider using it
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing binaries - excluding __DATE__ and __TIME__ strings bcg121 Linux - Software 2 12-16-2019 06:48 PM
c++ - if/else problem comparing strings babag Programming 14 05-19-2008 11:32 PM
comparing 2 strings in shell script dhanabalanb Programming 3 08-01-2007 01:17 PM
php problem with strings graziano1968 Programming 5 10-07-2005 02:10 PM
Error comparing strings and acting on the comparison WindowsBurner Programming 4 10-21-2004 12:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration