Dorian Waite Dorian Waite - 6 months ago 22
Javascript Question

Can't seem to remove extra whitespace in php or javascript

hey guys =) I'm having a small problem. I have some php code which gets a string stored in a database as favoritegamelist, and in the database, it doesn't appear to have any extra white space. However when I try to echo the string out it generates a bunch of new line characters. I used javascript to get the character code of the characters creating space and it was 10 and that's new line?
Either way, i figured a preg_replace function would work, or maybe trim? So I tried trim, to no avail. Currently i have this code:

$gamelist = preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $row['favoritegamelist']);
echo $gamelist;


After this code executes, some javascript runs and collects the echo, then i log it to the console and this is where i see a ton of extra white space and I'm just stuck trying to get rid of it. The javascript code is here:

var mygames = document.getElementById("favoriteGamesDiv").innerHTML;
console.log(mygames);


favoriteGamesDiv is the area that the echo statement in php is applicable. And finally the console log that i get is:

Llamas in Distress,Bow Master Halloween,Squary,


With all that extra space... So I'm not sure what to do. Any help would be appreciated ^-^ Thanks for bearing with me

Answer

Try using this it would help you..

$gamelist = preg_replace('/\s+/', ' ',$row['favoritegamelist']));