Page 1 of 1

add white spaces in strings

Posted: Wed Aug 20, 2008 12:51 pm
by bouncer
hi there,

is it possible to add white spaces in strings, like for instance if i have this string "abcdefghi", and add white spaces to have this "abc def ghi" ? :roll:

thanks in advance

Re: add white spaces in strings

Posted: Wed Aug 20, 2008 12:53 pm
by ghurtado
It is definitely possible. I have even added other characters to strings before with much success.

Re: add white spaces in strings

Posted: Wed Aug 20, 2008 3:11 pm
by dajawu
That one made me laugh, thanks! But seriously if your trying to put more then one space in a PHP string or HTML you should use  

Re: add white spaces in strings

Posted: Wed Aug 20, 2008 3:23 pm
by ghurtado
:D

Re: add white spaces in strings

Posted: Thu Aug 21, 2008 4:17 am
by bouncer
dajawu wrote:That one made me laugh, thanks! But seriously if your trying to put more then one space in a PHP string or HTML you should use
i have this piece of code ...

Code: Select all

 
$query = "SELECT * FROM `tbl` WHERE `field1`='$something''";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $query = "SELECT * FROM `tbl2` WHERE `field2`=".$row['value'];
    (...)
}
 
the value in field2 is a string with 9 digits like "xxx xxx xxx", and the $row['value'] is a string like "xxxxxxxxx", so i need a way to compare this two values, can you show me how can i do it ?

i've come up with this, but maybe this isn't the best way of doing it ...

Code: Select all

 
$a = substr($row['value'], 0, 3);
$b = substr($row['value'], 3, 3);
$c = substr($row['value'], 6, 3);
$value = $a." ".$b." ".$c;
 
thanks in advance

Re: add white spaces in strings

Posted: Thu Aug 21, 2008 4:52 am
by onion2k
bouncer wrote:

Code: Select all

 
$query = "SELECT * FROM `tbl` WHERE `field1`='$something''";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $query = "SELECT * FROM `tbl2` WHERE `field2`=".$row['value'];
    (...)
}
 
Completely off topic here, but as a general rule you shouldn't ever need to do a select query while you're looping through the results of another select. I can see that you could replace your code there with a join...

Code: Select all

$query = "SELECT * FROM `tbl` RIGHT JOIN `tbl2` ON `tbl`.`value` = `tbl2`.`field2` WHERE `field1`='$something''";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 //stuff
}
Using a join rather than lots and lots of select queries will make your script a lot faster.