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
Share on other sites

should have made the question clearer he was writen it like that and the project is well over 500 files its huge and would take me weeks to do it by hand so i was sorta hopeing there was an automated way of doing it

Link to comment
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
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
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
Share on other sites

I changed over 2300 lines of ?> html <?php to echo fast, so why can't you do 500 files? (100lines ea. would be 5000 lines).

 

Anyway, if its super huge I would think it's fine the way it is. I just switched mine for looks.

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.