Problem with string operation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
szms
Forum Contributor
Posts: 101
Joined: Thu Jun 26, 2003 12:23 pm

Problem with string operation

Post by szms »

Please take a look of the following code. While outputting the two string variables, I am getting the same sting with same amount of space. But while comaparing I am getting the output "Different" What kind of string operation I have to do so that I will get "Same". Thank you.

Code: Select all

<?php
$name1 = "     Hello          World        ");
$name2 = "Hello World";

print "Before: $name1<br>";
print "Before: $name2<br>";

if ($name1 == $name2)

    print "Same";

else
    print "Different";

?>
User avatar
Orkan
Forum Commoner
Posts: 32
Joined: Sun Aug 24, 2003 9:07 am
Location: Ukraine
Contact:

Post by Orkan »

If you take a look to your HTML code - you'll see that there ARE spaces... But IE cuts all spaces so that only one left. If you want to make more than one space - use &nbsp; comand instead of " ".
PHP tells you "different" cause they ARE different!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Try this

Code: Select all

// Sorry, my code didn't actually work, deleted it to save confusion
Mark
Last edited by JayBird on Tue Aug 26, 2003 9:46 am, edited 3 times in total.
szms
Forum Contributor
Posts: 101
Joined: Thu Jun 26, 2003 12:23 pm

Post by szms »

little example with code will help to understand better.
User avatar
Orkan
Forum Commoner
Posts: 32
Joined: Sun Aug 24, 2003 9:07 am
Location: Ukraine
Contact:

Post by Orkan »

Example:
if you write in file.html:

Code: Select all

something           anything        wateva
bla
you'll get:

Code: Select all

something anything wateva bla
to get

Code: Select all

something    anything    wateva    bla
use '& n b s p ;' w/o spaces between letters, not ' '!
(I placed spaces cause bug in forum accepts this text as html code, w/o converting special chars :()
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

that isn't really what he is asking if you re-read the question. He want to know how to make these two line equal each other

Code: Select all

$name1 = "     Hello          World        "); 
$name2 = "Hello World";
What operation can be done so that $name1 and $name2 equal each other.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm not quite sure what this is good for but anyway ;)

Code: Select all

<?php
$name1 = "     Hello          World        ";
$name2 = "Hello World";

// ignoring all spaces
// even if name2 is 'HelloWorld' this will print 'Same'
if (str_replace(' ', '', $name1) == str_replace(' ', '', $name2))
	print "Same";
else
	print "Different";
	
// removing whitespaces from start/end, then remove multiple-spaces within string
// if name2 is 'HelloWorld' this will print 'Different'
if ( preg_replace('/\s+/', ' ', trim($name1)) == preg_replace('/\s+/', ' ', trim($name2)) )
	print "Same";
else
	print "Different";	
?>
Post Reply