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!
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.
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 comand instead of " ".
PHP tells you "different" cause they ARE different!
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 )
<?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";
?>