Jump to content

html to echo


dingus

Recommended Posts

hey guy i got a problem i just taken over a project from a friend and he has it his code setup like this

 

<?php
$test = "some value";
?>
<table>
<tr>
<td>
<?php
echo $test ;
?>
</td>
</tr>
</table>

 

 

now the problem is i need to wrap that in to a single echo any ideas how i would do that (probably some basic regex but idk)

Link to comment
https://forums.phpfreaks.com/topic/113443-html-to-echo/
Share on other sites

ok this is just a basic example this is the origional code

 

 

<table border="0" cellpadding="0" cellspacing="0" width="100%" style="margin: ">
<tr bgcolor="#AFC0D0" valign="top">
<td style="width:33%">

					<strong><img src="images/e06.gif" style="width:16; height:9" alt="" border="0">Search for Business in Sydney</strong>
                           <fieldset>
					    <LEGEND></LEGEND>
                              <span><em><strong>1.</strong></em> <strong>Enter Suburbs:</strong></span><br>
                               <input id="tb" name="tb" value="" class="enterSuburb" tabindex="1" onkeyup="javascript:sfsComplete();" type="text"><br>
                                 <label class="selectSuburbs"><strong>Or Select Suburbs:</strong></label>
                                 <p>

                                    <span>
                                    <select id="u" name="u" size="5" class="suburbsList" onclick="javascript:sfsInsert();" style="width:75% ">
                                       <option selected="selected" value="">
                                          *** SHOW ALL SUBURBS ***
                                       </option>
<?
$query =
    	    "SELECT *
     	  	 FROM location
		 ORDER BY `location` ASC ";

$result = mysql_query($query);

// Determine the number of Users with that name
$number = mysql_num_rows($result);
if ($number == 0) {
echo "<meta http-equiv='Refresh' content='0; URL=index.php'>";
die () ;
} else {
   // Print the employee names
   for($i=0; $i<$number; $i++){
	   $Location = mysql_result($result,$i,"Location"); 
?>
   <option value="<? print $Location ; ?>"><? print $Location ; ?></option>
<?
  	}
}
?>
                                    </select>
					   </span>                           </p>
                                 <p>
                                    <span class="surroundingSuburbs"><input id="is" name="is" value="1" tabindex="20" type="checkbox"> Include Surrounding Suburbs</span>
                                 </p><br>
                  </fieldset></td>
<td><img src="images/e03.gif" width="15" height="298" alt="" border="0"></td>
<td bgcolor="#D1D6DB" style="width:33%; background-image:url(images/fon01.jpg); background-position: top; background-repeat: repeat-x;">


					<strong><img src="images/e06.gif" style="width:16; height:9" alt="" border="0">Search for Business by Category</strong>
                           <fieldset>
					    <LEGEND></LEGEND>
                            <label><em><strong>2.</strong></em> <strong>Enter Category:</strong></label> <span><br>
                                 <input id="Type_Category" name="Type_Category" value="" class="enterSuburb" tabindex="1" onkeyup="javascript:Category_sfsComplete();" type="text"></span><br>
                                 <label class="selectSuburbs"><strong>Or Select Category:</strong></label>
                                 <p>
                                    <span>
                                    <select id="Select_Category" name="Select_Category" size="5" class="suburbsList" onclick="javascript:Category_sfsInsert();" style="width:75% ">
                                       <option>
                                          *** Select Category Below ***
                                       </option>
<?
$query =
    	    "SELECT *
     	  	 FROM catagory
		 ORDER BY `catagory` ASC";

$result = mysql_query($query);

// Determine the number of Users with that name
$number = mysql_num_rows($result);
if ($number == 0) {
echo "<meta http-equiv='Refresh' content='0; URL=index.php'>";
die () ;
} else {
   // Print the employee names
   for($i=0; $i<$number; $i++){
	   $catagory = mysql_result($result,$i,"catagory"); 
?>
   <option value="<? print $catagory ; ?>"><? print $catagory ; ?></option>
<?
  	}
}
?>
								   
                           </select></span>                           </p>
                                 <p>
                                    <span class="Allcategorys"><input id="all_Category" name="all_Category" value="1" tabindex="20" type="checkbox"> Select All Category</span>
                                 </p><br>
                           </fieldset>			<p align="right"><input type="submit" value="search"></p>      </td>
<td><img src="images/e04.gif" width="14" height="298" alt="" border="0"></td>
<td width="33%"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
<tr>
<td colspan="5"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
<tr bgcolor="#EE7B10">
<td height="19" colspan="5"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
</table>

 

 

this is what i want to change it to

 

 

<?php
echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="margin: ">
<tr bgcolor="#AFC0D0" valign="top">
<td style="width:33%">

					<strong><img src="images/e06.gif" style="width:16; height:9" alt="" border="0">Search for Business in Sydney</strong>
                           <fieldset>
					    <LEGEND></LEGEND>
                              <span><em><strong>1.</strong></em> <strong>Enter Suburbs:</strong></span><br>
                               <input id="tb" name="tb" value="" class="enterSuburb" tabindex="1" onkeyup="javascript:sfsComplete();" type="text"><br>
                                 <label class="selectSuburbs"><strong>Or Select Suburbs:</strong></label>
                                 <p>

                                    <span>
                                    <select id="u" name="u" size="5" class="suburbsList" onclick="javascript:sfsInsert();" style="width:75% ">
                                       <option selected="selected" value="">
                                          *** SHOW ALL SUBURBS ***
                                       </option>';

$query =
    	    "SELECT *
     	  	 FROM location
		 ORDER BY `location` ASC ";

$result = mysql_query($query);

// Determine the number of Users with that name
$number = mysql_num_rows($result);
if ($number == 0) {
echo "<meta http-equiv='Refresh' content='0; URL=index.php'>";
die () ;
} else {
   // Print the employee names
   for($i=0; $i<$number; $i++){
	   $Location = mysql_result($result,$i,"Location"); 

   echo  '<option value="'. $Location .'">'. $Location .'</option>';

  	}
}

echo'                                    </select>
					   </span>                           </p>
                                 <p>
                                    <span class="surroundingSuburbs"><input id="is" name="is" value="1" tabindex="20" type="checkbox"> Include Surrounding Suburbs</span>
                                 </p><br>
                  </fieldset></td>
<td><img src="images/e03.gif" width="15" height="298" alt="" border="0"></td>
<td bgcolor="#D1D6DB" style="width:33%; background-image:url(images/fon01.jpg); background-position: top; background-repeat: repeat-x;">


					<strong><img src="images/e06.gif" style="width:16; height:9" alt="" border="0">Search for Business by Category</strong>
                           <fieldset>
					    <LEGEND></LEGEND>
                            <label><em><strong>2.</strong></em> <strong>Enter Category:</strong></label> <span><br>
                                 <input id="Type_Category" name="Type_Category" value="" class="enterSuburb" tabindex="1" onkeyup="javascript:Category_sfsComplete();" type="text"></span><br>
                                 <label class="selectSuburbs"><strong>Or Select Category:</strong></label>
                                 <p>
                                    <span>
                                    <select id="Select_Category" name="Select_Category" size="5" class="suburbsList" onclick="javascript:Category_sfsInsert();" style="width:75% ">
                                       <option>
                                          *** Select Category Below ***
                                       </option>';

$query =
    	    "SELECT *
     	  	 FROM catagory
		 ORDER BY `catagory` ASC";

$result = mysql_query($query);

// Determine the number of Users with that name
$number = mysql_num_rows($result);
if ($number == 0) {
echo "<meta http-equiv='Refresh' content='0; URL=index.php'>";
die () ;
} else {
   // Print the employee names
   for($i=0; $i<$number; $i++){
	   $catagory = mysql_result($result,$i,"catagory"); 

echo'	   <option value="'. $catagory .'">'. $catagory .'</option>';

  	}
}
echo'
								   
                           </select></span>                           </p>
                                 <p>
                                    <span class="Allcategorys"><input id="all_Category" name="all_Category" value="1" tabindex="20" type="checkbox"> Select All Category</span>
                                 </p><br>
                           </fieldset>			<p align="right"><input type="submit" value="search"></p>      </td>
<td><img src="images/e04.gif" width="14" height="298" alt="" border="0"></td>
<td width="33%"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
<tr>
<td colspan="5"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
<tr bgcolor="#EE7B10">
<td height="19" colspan="5"><img src="images/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
</table>';

 

 

basicly convertting from what was closeing the php tags to putting all the html in an echo statment

 

now for doing the converion i have code to open the files read in the data and write it to the new folder all i need is a a function to accualy convert it

Link to comment
https://forums.phpfreaks.com/topic/113443-html-to-echo/#findComment-583272
Share on other sites

From my testing, using single quotes is faster then going in and out of PHP/double quotes. However going in and out of PHP is faster then double quotes. Benchmark script:

<?php
function timer()
{
    list($usec, $sec) = explode(' ', microtime());
    return ((float)$usec + (float)$sec);
}

$iterations = 1000;

$t1 = timer();
echo '<div style="width:250px;height:200px;overflow:auto;margin:10px;border:1px solid #F00;">';
for($i = 0; $i < $iterations; $i++)
{
?>
Line #<?php echo $i; ?><br />
<?php
}
echo '</div>';

$t2 = timer();
echo '<div style="width:250px;height:200px;overflow:auto;margin:10px;border:1px solid #F00;">';
for($i = 0; $i < $iterations; $i++)
{
    echo 'Line #' . $i . '<br />';
}
echo '</div>';

$t3 = timer();
echo '<div style="width:250px;height:200px;overflow:auto;margin:10px;border:1px solid #F00;">';
for($i = 0; $i < $iterations; $i++)
{
    echo "Line #$i<br />";
}
echo '</div>';

$t4 = timer();

$total1 = $t2-$t1;
$total2 = $t3-$t2;
$total3 = $t4-$t3;

$total = $t4-$t1;

echo "in-out PHP :  $total1 <br />";
echo "in PHP (single quotes) :  $total2 <br />";
echo "in PHP (double quotes):  $total3 <br /><br />";

echo "Total :  $total <br />";
?>

 

My Results:

in-out PHP : 0.0014281272888184

in PHP (single quotes) : 0.0012688636779785

in PHP (double quotes): 0.0019800662994385

 

Total : 0.0046770572662354

Link to comment
https://forums.phpfreaks.com/topic/113443-html-to-echo/#findComment-583804
Share on other sites

The performance difference is absolutely negligible. (did I spell that right?)

 

Placing all of the HTML into strings not only becomes a maintenance nightmare (costs more money to "own", because it takes more time to fix [time=money]).

 

Also, you run into memory/string handling stuff...

 

If you're having performance issues "going in and out of PHP" then it's time to look into using a Cache mechanism, because the other gains are not worth your time.

 

You will acheive *vastly greater results* looking into other means of optimization. (Cache on PHP, Cache on Database information, Query Optimization, etc).

 

Please, please, I beg you not to put all of that HTML into strings.

Link to comment
https://forums.phpfreaks.com/topic/113443-html-to-echo/#findComment-583992
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.