Prepare a block of text to display in php

Prepare a block of text to display in php

I need to append the contents of a text file to a string in php line by line. awk and sed are very useful for such things.

Here is the content of the file. It’s a fairly standard bit of bootstrap HTML.

<div class="row">
    <div class="col-sm-12">
        <ul class="nav nav-tabs nav-tabs-module" style="margin-left: 30px;">
            <li class="active"><a data-toggle="tab" href="#module_tab_0">TAB 1</a></li>
            <li><a data-toggle="tab" href="#module_tab_1">TAB 2</a></li>
        </ul>
    </div>
</div>
<div class="tab-content">
    <div id="module_tab_0" class="tab-pane active">
        <br />This is tab 1
    </div>
    <div id="module_tab_1" class="tab-pane">
        <br />Hello
        <br /><br />
        This is tab 2
    </div>
</div>

I need to replace all of the ” characters with ‘, then start each line with $string.=” and end each line with \n”;

cat divs.txt | sed "s/\"/'/g" | awk '{print "\$string.=\""$0}' | awk '{print $0"\\n\";"}'
  1. Cat divs.txt simply prints the content of the file.
  2. This is piped into the sed command which replaces all of the ” with ‘. Notice the escape character \” so that sed ignores the “
  3. This awk command places the $string.=” at the beginning of each line. Escape characters are required again on the $ and ” so that awk ignores them
  4. This awk appends \n”; to each line. This time I need to escape the \n otherwise awk just does a carriage return!

The result:

$string.="<div class='row'>\n";
$string.="               <div class='col-sm-12'>\n";
$string.="                                  <ul class='nav nav-tabs nav-tabs-module' style='margin-left: 30px;'>\n";
$string.="                                                   <li class='active'><a data-toggle='tab' href='#module_tab_0'>TAB 1</a></li>\n";
$string.="                                                   <li><a data-toggle='tab' href='#module_tab_1'>TAB 2</a></li>\n";
$string.="                                  </ul>\n";
$string.="                 </div>\n";
$string.="</div>\n";
$string.="\n";
$string.="<div class='tab-content'>\n";
$string.="                 <div id='module_tab_0' class='tab-pane active'>\n";
$string.="                                  <br />This is tab 1\n";
$string.="                 </div>\n";
$string.="                 <div id='module_tab_1' class='tab-pane'>\n";
$string.="                                  <br />Hello\n";
$string.="                                  <br /><br />\n";
$string.="                                  This is tab 2\n";
$string.="                 </div>\n";
$string.="</div>\n";

Leave a Reply

Your email address will not be published. Required fields are marked *

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.