add white spaces in strings

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
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

add white spaces in strings

Post 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
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: add white spaces in strings

Post by ghurtado »

It is definitely possible. I have even added other characters to strings before with much success.
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: add white spaces in strings

Post 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  
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: add white spaces in strings

Post by ghurtado »

:D
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: add white spaces in strings

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: add white spaces in strings

Post 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.
Post Reply