Question: Hi I encounter some trouble when trying to save php variable to mysql database from shell\_exec result.

I want to get my local ip system in ubuntu, so I use 

```
<?php
    $ip = shell_exec("/sbin/ifconfig eth0 | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}' | tr -d ' '");
?>

```

I insert the $ip variable to mysql database, apparently there are some hidden character from the exec result

```
+------------+-----------------+-------------------+------------+------------+
| monitor_id | ip_address      | total_transaction | total_user | total_hits |
+------------+-----------------+-------------------+------------+------------+
|         70 | 
10.151.32.187
 |                 5 |          0 |          4 |
+------------+-----------------+-------------------+------------+------------+

```

looks like there are so many whitespaces, I already trim the whitespace with the following syntax :

```
str_replace(" ","",$ip);

```

and many other way but it returns the same. Can anybody help? Please..

Answer: 
Try using the PHP function [trim](http://www.php.net//manual/en/function.trim.php). What it does:

> 
> Strip whitespace (or other characters) from the beginning and end of a
>  string.
> 
> 
>
`trim()` will replace all whitespace characters at the beginning and end of the string.

If you want to replace all whitespace characters, regardless of where they are in the string, you can use preg\_replace like so:

```
$string = preg_replace('/\s+/', '', $string);

```