3

I am building a new version of a telephone configuration manager where I am sucking on a stupid problem. You see these telephone .cfg configurations are rely static. So in the old version I made it gave the configuration without a problem.

It looks like this:

## Configuration header
configuration_1="parram"
configuration_2="parram"
configuration_3="parram"

etc.

Now in the new version the configuration is given as this:

whitespace
## Configuration header
configuration_1="parram"
configuration_2="parram"
configuration_3="parram"

Note that white space is actually white space and that the phone does not take the configuration, because it wants to see the first line have the #header.

So I figured that the easy way to fix this is to just backspace the first white line but how. How can I tell PHP to delete the first line?

OK, look at this: image

The first to screenshots are from phpMyAdmin where you see that inside an textarea there is no white space, but when just echoing it out you suddenly see it. The strange thing is that when manually changing the configuration with phpMyAdmin it is removed somehow, but it has be done automatically.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
botenvouwer
  • 4,334
  • 9
  • 46
  • 75
  • You can use a function to read the file line by line, and delete the first line. Would that help? – desbest Oct 25 '12 at 10:40
  • Where does this configuration come from? Can't you just edit the file? Otherwise, can you use [trim](http://php.net/manual/en/function.trim.php "PHP Docs") to trim whitespace of the string? – GolezTrol Oct 25 '12 at 10:41
  • I only need to easily remove the first line. Witch function can do this. PS the configurations are all diffident and dynamic no i cant do it Manually. The strange thing is that on first eye you can't detect white space but when it is inserted in database and then outputted directly with the php header plain text it suddenly shows white space. If I look in phpmyadmin I see white space but when i edit in text-box its gone. I make screenshot just a sec! – botenvouwer Oct 25 '12 at 10:53

6 Answers6

20

If you have the contents as a string, just run ltrim.
It will strip away all the whitespaces from the starting of the string.

$str = ltrim($str);
air4x
  • 5,618
  • 1
  • 23
  • 36
  • This does it. Great work I just figured that this was displayed in the see also on php.net-> trim function. But never mind this works for me! – botenvouwer Oct 25 '12 at 12:12
2

That is how to remove only the first whitespace:

$s   = '  Text';
$arr = str_split($s);
array_shift($arr);
$s   = implode('', $arr);

die($s);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
1

If you got this configuration in a string, as your title says, you can just trim the string.

$config = trim($config);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Well the problem is that the configuration needs to be so static. It needs 2 white spaces on the end and between beginning and end and also more white spaces. The only thing I have to do is to remove the fist white space. so it looks like example one I gave. PS, the white spaces come form a xml string. In the presses i have a xml header witch is removed. The problem is that it leaves a white space. – botenvouwer Oct 25 '12 at 10:49
  • Trim only removes whitespace on the begin and end of string. It won't remove all spaces from the string. Of course, if you need the ending whitespace, trim won't work (or you would have to add them yourself), but I couldn't know of a reason why they would be important. – GolezTrol Oct 25 '12 at 11:00
  • If you read the value from an XML, it may be better to use an [XML Parser](http://php.net/manual/en/book.simplexml.php "Like SimpleXML") that actually parses the XML instead of stripping tags and hoping the remainder is okay. – GolezTrol Oct 25 '12 at 11:01
0

I would delete up to the first hash -

$contents = substr($contents, 0, strpos($contents, "#") - 1)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
EdJ
  • 1,296
  • 1
  • 11
  • 16
0

You can use the PHP function trim.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Valeriy Gorbatikov
  • 3,459
  • 1
  • 15
  • 9
0

Put the following code on the first line of the file:

ob_start();

Put the following code just before you displaying the content:

ob_end_clean();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39