Question on whitespace

I am trying to build upon the entity converter that takes code and prints out any HTML entities. I have it all working. I am just curious about removing the whitespace while maintaining the code on each line. I know that sounds confusing but I’ll try and give an example.

The HTML:

<form method="post" > 
	<h3>Encode:</h3>
	<textarea name="codeArea">Enter your code here...</textarea>
	<input class="btn btn-primary" type="submit" value="Encode!">	   
</form

The PHP:
[php]

<?php $text = $_POST['codeArea']; $text = preg_replace('/\s\s+/', ' ', $text); $UserInputText = htmlentities($text); echo '
'.htmlentities($UserInputText).'
'; ?>

[/php]

Currently if I input:
[php]

<?php $cost = 8.50; function totalPrice(){ $tax = 2.50; echo "

Value of variables inside the function

"; echo "Variable cost is: $cost
"; echo "Variable tax is: $tax
"; } totalPrice(); echo "
"; echo "
"; echo "

Value of variables outside the function

"; echo "Variable cost is: $cost"; echo "
" // echo "Variable tax is: $tax"; ?>

[/php]

I want it to output the results without all the spacing but keep the code on separate lines

Currently I am getting this as the output:

&lt;?php $cost = 8.50; function totalPrice(){ $tax = 2.50; echo &quot;&lt;h2&gt;Value of variables inside the function&lt;/h2&gt;&quot;; echo &quot;Variable cost is: $cost &lt;br /&gt;&quot;; echo &quot;Variable tax is: $tax &lt;br /&gt;&quot;; } totalPrice(); echo &quot;&lt;br /&gt;&quot;; echo &quot;&lt;br /&gt;&quot;; echo &quot;&lt;h2&gt;Value of variables outside the function&lt;/h2&gt;&quot;; echo &quot;Variable cost is: $cost&quot;; echo &quot;&lt;br /&gt;&quot; // echo &quot;Variable tax is: $tax&quot;; ?&gt;

Can you show us how you want the output instead of describing it.

You want this

&lt;?php $cost = 8.50; function totalPrice(){ $tax = 2.50; echo &quot;&lt;h2&gt;Value of variables inside the function&lt;/h2&gt;&quot;; echo &quot;Variable cost is: $cost &lt;br /&gt;&quot;; echo &quot;Variable tax is: $tax &lt;br /&gt;&quot;; } totalPrice(); echo &quot;&lt;br /&gt;&quot;; echo &quot;&lt;br /&gt;&quot;; echo &quot;&lt;h2&gt;Value of variables outside the function&lt;/h2&gt;&quot;; echo &quot;Variable cost is: $cost&quot;; echo &quot;&lt;br /&gt;&quot; // echo &quot;Variable tax is: $tax&quot;; ?&gt;

To Become ?

Yes Sir… If I enter in:

               <?php

                                    $test1 = $test2 = $test3 = $test4 = "it worked"; 
                                       echo $test1 . "<br />"; 
                                       echo $test2 . "<br />"; 
                                       echo $test3 . "<br />"; 
                                       echo $test4 . "<br />"; 

                   ?>

I would ideally like the output to be :

&lt;?php
     $test1 = $test2 = $test3 = $test4 = &quot;it worked&quot;; 
     echo $test1 . &quot;&lt;br /&gt;&quot;; 
     echo $test2 . &quot;&lt;br /&gt;&quot;; 
     echo $test3 . &quot;&lt;br /&gt;&quot;; 
     echo $test4 . &quot;&lt;br /&gt;&quot;; 
?&gt;

Just basically wanting to keep the code structure but eliminate the excess whitespace. I have it converting fine but … I just need help with the whitespace. For some reason I can’t seem to wrap my brain around what I need to do to fix it.

Thaniks

The \s matches your line feeds and that’s converted to a space.

[php]$text = preg_replace(’/\s\s+/’, ’ ', $text);[/php]

You can try something like this. .

[php]$text = preg_replace("/[ ]+/", " ", $text );[/php]

or

[php]$text = preg_replace(’/[^\S\n]+/’, ’ ', $text );[/php]

Sorry for the delay in responding. Thanx much Top for the advice I’ll give it a shot.

I didnt look at your code in detail, but If I understand, just use the newline character to start a new line and ltrim to remove white space to the left. (There is also rtrim for right side only and just trim for left and right.)

[php]<?php
$string= " This is my my first line\nthis is my second line\nthis is my third line";
echo ltrim($string);
?>[/php]

[code]Output

This is my my first line
this is my second line
this is my third line[/code]

oh nice ty sir

Sponsor our Newsletter | Privacy Policy | Terms of Service