Jump to content

getting rid of spaces and tabs


enkidu72

Recommended Posts

Hi all ,

I have some problems cleaning a text file for output it in an HTML page ...

The source file contains something like this :

 

  ANGLES:                                         5-   10-   15-
#              Filename     nvio   max    <5    10    20    20   >20
---------------------------------------------------------------------------------------------------------------
  1                min.out     0   0.00     0     0     0     0     0


  OMEGAS:                                         5-   10-   15-
#              Filename     nvio   max    <5    10    20    20   >20
---------------------------------------------------------------------------------------------------------------
  1                min.out     0   0.00     0     0     0     0     0
---------------------------------------------------------------------------------------------------------------

Distance and angle violations:

First atom     Last atom    target   Key: . = 0.0  - <= 0.1  + <= 0.2  * <= 0.5  0 <= 1.0  8 <= 2.0  @ > 2.0  Stats: Ave <std> min/max/#
  CD2  TRP   43 --  H    PHE   30 :    6.00 : *   0.27  <  0.00>   0.27 /   0.27   1
  CD2  TRP   43 --  HA   LYS   31 :    3.30 : +   0.13  <  0.00>   0.13 /   0.13   1
  CZ3  TRP   43 --  HA   LYS   31 :    3.30 : *   0.35  <  0.00>   0.35 /   0.35   1
  CH2  TRP   43 --  HA   PHE   52 :    6.00 : *   0.31  <  0.00>   0.31 /   0.31   1
  CH2  TRP   43 --  H    THR   53 :    5.00 : +   0.17  <  0.00>   0.17 /   0.17   1
  CH2  TRP   43 --  HA   VAL   54 :    5.00 : *   0.23  <  0.00>   0.23 /   0.23   1
  HA   GLU   15 --  H    LEU    7 :    2.90 : +   0.11  <  0.00>   0.11 /   0.11   1
  H    GLU   27 --  H    LYS   28 :    2.90 : *   0.22  <  0.00>   0.22 /   0.22   1
  HD2  PHE   30 --  HA   THR   17 :    3.30 : 0   0.89  <  0.00>   0.89 /   0.89   1
  HA   LYS   31 --  H    ALA   34 :    3.50 : +   0.12  <  0.00>   0.12 /   0.12   1
  HD2  PHE   52 --  H    GLU   27 :    3.50 : *   0.37  <  0.00>   0.37 /   0.37   1
  HA   PHE   52 --  H    ASP   46 :    3.50 : *   0.20  <  0.00>   0.20 /   0.20   1

 

 

I just wanted to put the last part in a table , so I did this :

 

$viol=file("$path/viol.out");
$to_replace="0";
foreach ($viol as $line ){
    
    $is_match=preg_match('/violations/',$line);
    
    if ($is_match) {
        $to_replace="1";
        echo "<table>";
    }
    if ($to_replace==1){
        echo "<tr>";
        $line = eregi_replace(" +", " ", $line);
        $line=str_replace(array( "\r\n","\r", "\n", "\t"), '', $line);
        $string=explode(" ",$line);
        foreach ($string as $word){
                 echo "<td>$word</td>";
         }
        echo "</tr>";
    }else{
        echo "$line<br/>";
    }
}
echo "</table>"

 

 

But I have some extra chars at the beginning of the table ...

 

I almost solved it checking each word with trim() , but trim get rid of "0" too ... so I can't use it ...

 

 

Some ideas ?

 

 

Thx in advance

 

 

David

Link to comment
Share on other sites

hi flyhoney ,

thx for your reply , but that's not really the problem ...

I don't need to clean all blank spaces , I need them to explode the line ...

Later when I have to write the table , i need to ignore them .

 

What was ( almost as I said ) working was :

 


foreach ($string as $word){
            $check=trim($word);
            if (  !empty($check)) {
            echo "<td>$word</td>";
            }
        }

 

 

 

 

<table><tr><td>Distance</td><td>and</td><td>angle</td><td>violations:</td></tr><tr><td></td></tr><tr><td></td><td>target</td><td>Key:</td><td>.</td><td>=</td><td>0.0</td><td>-</td><td><=</td><td>0.1</td><td>+</td><td><=</td><td>0.2</td><td>*</td><td><=</td><td>0.5</td><td>0</td><td><=</td><td>1.0</td><td>8</td><td><=</td><td>2.0</td><td>@</td><td>></td><td>2.0</td><td>Stats:</td><td>Ave</td><td><std></td><td>min/max/#</td></tr><tr><td></td><td>CD2</td><td>TRP</td><td>43</td><td>--</td><td>H</td><td>PHE</td><td>30</td><td>:</td><td>6.00</td><td>:</td><td>*</td><td>0.44</td><td><</td><td>0.00></td><td>0.44</td><td>/</td><td>0.44</td><td>1</td></tr><tr><td></td><td>CD2</td><td>TRP</td><td>43</td><td>--</td><td>HA</td><td>LYS</td><td>31</td><td>:</td><td>3.30</td><td>:</td><td>+</td><td>0.15</td><td><</td><td>0.00></td><td>0.15</td><td>/</td><td>0.15</td><td>1</td></tr><tr><td></td><td>CZ3</td><td>TRP</td><td>43</td><td>--</td><td>HA</td><td>LYS</td><td>31</td><td>:</td><td>3.30</td><td>:</td><td>*</td><td>0.21</td><td><</td><td>0.00></td><td>0.21</td><td>/</td><td>0.21</td><td>1</td></tr><tr><td></td><td>CH2</td><td>TRP</td><td>43</td><td>--</td><td>HA</td><td>PHE</td><td>52</td><td>:</td><td>6.00</td><td>:</td><td>*</td><td>0.32</td><td><</td><td>0.00></td><td>0.32</td><td>/</td><td>0.32</td><td>1</td></tr><tr><td></td><td>CH2</td><td>TRP</td><td>43</td><td>--</td><td>HA</td><td>VAL</td><td>54</td><td>:</td><td>5.00</td><td>:</td><td>*</td><td>0.28</td><td><</td><td>0.00></td><td>0.28</td><td>/</td><td>0.28</td><td>1</td></tr><tr><td></td><td>H</td><td>ALA</td><td>26</td><td>--</td><td>H</td><td>GLU</td><td>27</td><td>:</td><td>2.90</td><td>:</td><td>-</td><td>0.07</td><td><</td><td>0.00></td><td>0.07</td><td>/</td><td>0.07</td><td>1</td></tr><tr><td></td><td>H</td><td>GLU</td><td>27</td><td>--</td><td>H</td><td>LYS</td><td>28</td><td>:</td><td>2.90</td><td>:</td><td>*</td><td>0.26</td><td><</td><td>0.00></td><td>0.26</td><td>/</td><td>0.26</td><td>1</td></tr><tr><td></td><td>HD2</td><td>PHE</td><td>30</td><td>--</td><td>HA</td><td>THR</td><td>17</td><td>:</td><td>3.30</td><td>:</td><td>0</td><td>0.94</td><td><</td><td>0.00></td><td>0.94</td><td>/</td><td>0.94</td><td>1</td></tr><tr><td></td><td>HA</td><td>LYS</td><td>31</td><td>--</td><td>H</td><td>ALA</td><td>34</td><td>:</td><td>3.50</td><td>:</td><td>+</td><td>0.15</td><td><</td><td>0.00></td><td>0.15</td><td>/</td><td>0.15</td><td>1</td></tr><tr><td></td><td>H</td><td>LYS</td><td>50</td><td>--</td><td>H</td><td>THR</td><td>51</td><td>:</td><td>2.90</td><td>:</td><td>-</td><td>0.09</td><td><</td><td>0.00></td><td>0.09</td><td>/</td><td>0.09</td><td>1</td></tr><tr><td></td><td>H</td><td>PHE</td><td>52</td><td>--</td><td>H</td><td>LYS</td><td>4</td><td>:</td><td>3.50</td><td>:</td><td>-</td><td>0.08</td><td><</td><td>0.00></td><td>0.08</td><td>/</td><td>0.08</td><td>1</td></tr><tr><td></td><td>HD2</td><td>PHE</td><td>52</td><td>--</td><td>H</td><td>GLU</td><td>27</td><td>:</td><td>3.50</td><td>:</td><td>*</td><td>0.39</td><td><</td><td>0.00></td><td>0.39</td><td>/</td><td>0.39</td><td>1</td></tr><tr><td></td><td>HA</td><td>PHE</td><td>52</td><td>--</td><td>H</td><td>ASP</td><td>46</td><td>:</td><td>3.50</td><td>:</td><td>*</td><td>0.23</td><td><</td><td>0.00></td><td>0.23</td><td>/</td><td>0.23</td><td>1</td></tr></table>    

</div>

Link to comment
Share on other sites

So is the problem that the number of spaces between the data isn't fixed, so it's difficult to explode? If so, you might care to try split - you can explode by a regular expression with that, so you don't need to know how many spaces there are.

 

$words = preg_split("/[\s,]+/", "This will split on        any      number                   of spaces");
print_r($words);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.