0

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

10
  • in php just use trim... Example Commented May 25, 2016 at 6:42
  • I tried trim, however it didn't work Commented May 25, 2016 at 6:43
  • Your favoriteGamesDiv probably has some format newlines/padding. So if you get the innerHTML, you will get all the whitespace as well. Commented May 25, 2016 at 6:45
  • IN javascript: Example Commented May 25, 2016 at 6:45
  • 2
    What is the element for favoriteGamesDiv is that a textarea or input Commented May 25, 2016 at 6:52

4 Answers 4

1

Try using this it would help you..

$gamelist = preg_replace('/\s+/', ' ',$row['favoritegamelist']));
Sign up to request clarification or add additional context in comments.

1 Comment

Also you can use below for javascript : myStr = myStr.replace(/\s{2,}/g,' ');
1

Using the trim of both PHP and JavaScript you can did what you want to do. Replace the $str with your real variable.

PHP

$str="                                Llamas in Distress,Bow Master Halloween,Squery, ";
echo trim($str);

Online Example

JavaScript

var str = "                                Llamas in Distress,Bow Master Halloween,Squery, ";
console.log(str.trim());

Online Example

Let me know if anything else I can do for you.

Comments

0

I didn't solve the problem with trim or preg_replace in either language, however I ran it through a javascript function that I made to substring the values out if the charcode was from 0-31... Thanks guys =)

Comments

0

Trimming array values with trim()

     <?php function trim_value(&$value) 
     { 
           $value = trim($value); 
      }

  $fruit = array('apple','banana ', ' cranberry ');
 var_dump($fruit);

       array_walk($fruit, 'trim_value');
     var_dump($fruit);

   ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.