Web Automation, from The PHP Cookbook -WebReference- | 8 | WebReference

Web Automation, from The PHP Cookbook -WebReference- | 8

PHP Cookbook: Web Automation

[The following is the conclusion of our series of excerpts from Chapter 11 of the O'Reilly title, PHP Cookbook.]

Removing HTML and PHP Tags

Problem

You want to remove HTML and PHP tags from a string or file.

Solution

Use strip_tags( ) to remove HTML and PHP tags from a string:

$html = '<a href="http://www.oreilly.com">I <b>love computer books.</b></a>';
print strip_tags($html);
I love computer books.

Use fgetss( ) to remove them from a file as you read in lines:

$fh = fopen('test.html','r') or die($php_errormsg);
while ($s = fgetss($fh,1024)) {
    print $s;
}
fclose($fh)                  or die($php_errormsg);

Discussion

While fgetss( ) is convenient if you need to strip tags from a file as you read it in, it may get confused if tags span lines or if they span the buffer that fgetss( ) reads from the file. At the price of increased memory usage, reading the entire file into a string provides better results:

$no_tags = strip_tags(join('',file('test.html')));

Both strip_tags( ) and fgetss( ) can be told not to remove certain tags by specifying those tags as a last argument. The tag specification is case-insensitive, and for pairs of tags, you only have to specify the opening tag. For example, this removes all but <b></b> tags from $html:

$html = '<a href="http://www.oreilly.com">I <b>love</b> computer books.</a>';
print strip_tags($html,'<b>');
I <b>love</b> computer books.

See Also

Documentation on strip_tags( ) at http://www.php.net/strip-tags and fgetss( ) at http://www.php.net/fgetss.

Using Smarty Templates

Problem

You want to separate code and design in your pages. Designers can work on the HTML files without dealing with the PHP code, and programmers can work on the PHP files without worrying about design.

Solution

Use a templating system. One easy-to-use template system is called Smarty. In a Smarty template, strings between curly braces are replaced with new values:

Hello, {$name}

The PHP code that creates a page sets up the variables and then displays the template like this:

require 'Smarty.class.php';
 
$smarty = new Smarty;
$smarty->assign('name','Ruby');
$smarty->display('hello.tpl');

Discussion

Here's a Smarty template for displaying rows retrieved from a database:

<html>
<head><title>cheeses</title></head>
<body>
<table border="1">
<tr>
  <th>cheese</th>
  <th>country</th>
  <th>price</th>
</tr>
{section name=id loop=$results}
<tr>
  <td>{$results[id]->cheese}</td>
  <td>{$results[id]->country}</td>
  <td>{$results[id]->price}</td>
</tr>
{/section}        
</table>
</body>
</html>

Here's the corresponding PHP file that loads the data from the database and then displays the template, stored in food.tpl:

require 'Smarty.class.php';
 
mysql_connect('localhost','test','test');
mysql_select_db('test');
 
$r = mysql_query('SELECT * FROM cheese');
while ($ob = mysql_fetch_object($r)) {
    $ob->price = sprintf('$%.02f',$ob->price);
    $results[] = $ob;
 
}
$smarty = new Smarty;
$smarty->assign('results',$results);
$smarty->display('food.tpl');

After including the base class for the templating engine (Smarty.class.php), you retrieve and format the results from the database and store them in an array. To generate the templated page, just instantiate a new $smarty object, tell $smarty to pay attention to the $results variable, and then tell $smarty to display the template.

Smarty is easy to install: just copy a few files to your include_path and make a few directories. Use Smarty with discipline to preserve the value of having templates in the first place--separating your logic and your presentation. A template engine has its own scripting language you use to interpolate variables, execute loops, and do other simple logic. Try to keep that to a minimum in your templates and load up your PHP files with the programming.

See Also

The Smarty home page at http://smarty.php.net/.


Created: March 27, 2003
Revised: March 27, 2003

URL: http://webreference.com/programming/php/chap11/2