blog.scriptdigital.com [Archives]

Emmanuel M Décarie's weblog on scripting.
Mostly PHP, Radio UserLand, Perl, Applescript and OS X.
RSS/XML Feed | scriptdigital.com | Picture | Search
Categories: AppleScript | BBEdit | Internet | Javascript | OS X | Perl | PHP | Python | Review | Radio UserLand
Technorati URL search for http://blog.scriptdigital.com.
Search Technorati for information relating to the URL or keyword of your choice.
... Original post: blog.scriptdigital.com: Emmanuel M Décarie's weblog on scripting. by at Daypop Search - filter replacement ...
Filter Replacement View Technorati URL search

PHP

Thu 05 Jun 2003

PHP Tip: Formatting Text

It happens all the time. You have for example a form that let your users leave a comment and you want to format the text to display it in a web page. Or you have some text in a database that you want to display also on a web page.

The text is not already formatted for a web page. You want to change accented characters in HTML entities, and format each paragraph inside <p></p>, and add a break line (<br />) after each carriage return. You may also want your users to be able to input only some HTML tags like <strong><b><em><i>.

Here's a little function called formatText that do all the above.

Listing: function formatText

function formatText ($text, $addPara=1, $addBreakLine=1, $stripTags=0, $allowedTags='') {

    $text = trim ($text);
    
    # translate all accented characters to HTML entities
    # but keep all HTML tags
    $table = get_html_translation_table (HTML_ENTITIES);
    $table['<'] = '<';
    $table['>'] = '>';
    $text = strtr ($text, $table);
    
    # strip all HTML tags if the param $stripTags is set to 1
    # keep the tags that are set in the param $allowedTags
    if ($stripTags) {
      if ($allowedTags) {
        $text = strip_tags ($text, $allowedTags);
      } else {
        $text = strip_tags ($text);
      }
    }
    
    # add <p> if the param $addPara is set to 1 (default)
    if ($addPara) {
      $textElements = preg_split ("/\n(\s)+/U", $text);
      $newText = "";
      foreach ($textElements as $item) {
        #be sure to strip control chars
        $item = trim ($item);
        if ( strlen ($item) != 0 ) {
          $newText .= "<p>$item</p>";
        }
      }
      $text = $newText;
    } 
    
    # add <br> if the param $addBreakLine is set to 1 (default)
    if ($addBreakLine) {
      $table      = null;
      $text       = nl2br($text);
      # nl2br will strip the 'n' after the </p>, add it here 
      # to have a nice formatted HTML source
      $table["</p>"]  = "</p>\n\n";
      $text       = strtr ($text, $table);
    }
    
  return $text;
}

And here's how to use it.
$rawText is in the ISO-8859-1 format. I'm using it for the examples below.

Listing: $rawText

$rawText = <<< END
This is the first paragraph with one sentence.

This is the second paragraph with its first sentence.
This is the second sentence of the second paragraph.

Et voilà un paragraphe avec des caractères accentués.

This is the <em>last sentence</em> <strong>with HTML tags</strong>.
END;

 

Example 1: formatText ($displayText)

Example 1: adding paragraphs and break lines (default)
This is the first paragraph with one sentence.

This is the second paragraph with its first sentence.
This is the second sentence of the second paragraph.

Et voilà un paragraphe avec des caractères accentués.

This is the last sentence with HTML tags.


$exampleTitle	= "<strong>Example 1: adding paragraphs and break lines (default)</strong>";
$displayText	= $exampleTitle . "\n" . $rawText . "<hr>";
echo formatText ($displayText);

-->
<p><strong>Example 1: adding paragraphs and break lines (default)</strong><br />
This is the first paragraph with one sentence.</p>

<p>This is the second paragraph with its first sentence.<br />
This is the second sentence of the second paragraph.</p>

<p>Et voil&agrave; un paragraphe avec des caract&egrave;res accentu&eacute;s.</p>

<p>This is the <em>last sentence</em> <strong>with HTML tags</strong>.<hr></p>

 

Example 2: formatText ($displayText, 0, 1)

Example 2: not adding paragraphs but adding break lines
This is the first paragraph with one sentence.

This is the second paragraph with its first sentence.
This is the second sentence of the second paragraph.

Et voilà un paragraphe avec des caractères accentués.

This is the last sentence with HTML tags.
$exampleTitle	= "<strong>Example 2: not adding paragraphs but adding break lines</strong>";
$displayText	= $exampleTitle . "\n" . $rawText . "<hr>";
echo formatText ($displayText, 0, 1);

-->
<strong>Example 2: not adding paragraphs but adding break lines</strong><br />
This is the first paragraph with one sentence.<br />
<br />
This is the second paragraph with its first sentence.<br />
This is the second sentence of the second paragraph.<br />
<br />
Et voilà un paragraphe avec des caractères accentués.<br />
<br />
This is the <em>last sentence</em> <strong>with HTML tags</strong>.<hr>

 

Example 3: formatText ($displayText, 0, 0)

Example 3: not adding paragraphs nor break lines This is the first paragraph with one sentence. This is the second paragraph with its first sentence. This is the second sentence of the second paragraph. Et voilà un paragraphe avec des caractères accentués. This is the last sentence with HTML tags.
$exampleTitle	= "<strong>Example 3: not adding paragraphs nor break lines</strong>";
$displayText	= $exampleTitle . "\n" . $rawText . "<hr>";
echo formatText ($displayText, 0, 0);

-->
<strong>Example 3: not adding paragraphs nor break lines</strong>
This is the first paragraph with one sentence.

This is the second paragraph with its first sentence.
This is the second sentence of the second paragraph.

Et voilà un paragraphe avec des caractères accentués.

This is the <em>last sentence</em> <strong>with HTML tags</strong>.<hr>

 

Example 4: formatText ($displayText, 1, 1, 1, '<hr>')

Example 4: adding paragraphs, strip html tags but keep only the rule HTML tag
This is the first paragraph with one sentence.

This is the second paragraph with its first sentence.
This is the second sentence of the second paragraph.

Et voilà un paragraphe avec des caractères accentués.

This is the last sentence with HTML tags.


$exampleTitle	= "<strong>Example 4: adding paragraphs and break lines, 
                   strip html tags but keep only the rule HTML tag</strong>";
$displayText	= $exampleTitle . "\n" . $rawText . "<hr>";
echo formatText ($displayText, 1, 1, 1, '<hr>');

-->
<p>Example 4: adding paragraphs, strip html tags but keep only the rule tag HTML tag<br />
This is the first paragraph with one sentence.</p>

<p>This is the second paragraph with its first sentence.<br />
This is the second sentence of the second paragraph.</p>

<p>Et voilà un paragraphe avec des caractères accentués.</p>

<p>This is the last sentence with HTML tags.<hr></p>

 

Example 5: formatText ($displayText, 1, 1, 1)

Example 5: adding paragraphs, adding break lines, strip html tags
This is the first paragraph with one sentence.

This is the second paragraph with its first sentence.
This is the second sentence of the second paragraph.

Et voilà un paragraphe avec des caractères accentués.

This is the last sentence with HTML tags.

$exampleTitle	= "<strong>Example 5: adding paragraphs, 
                   adding break lines, strip html tags</strong>";
$displayText	= $exampleTitle . "\n" . $rawText . "<hr>";
echo formatText ($displayText, 1, 1, 1);

-->
<p>Example 5: adding paragraphs, adding break lines, strip html tags<br />
This is the first paragraph with one sentence.</p>

<p>This is the second paragraph with its first sentence.<br />
This is the second sentence of the second paragraph.</p>

<p>Et voilà un paragraphe avec des caractères accentués.</p>

<p>This is the last sentence with HTML tags.</p>

 

As you can see, this function is far from perfect since it will enclose inside <p></p> the <hr> tag. But beside that, it work well for me.

comments: 0

11:36AM EDT [ /PHP | # ]

Sun 11 May 2003

PHP Tip: Named Parameters

The PHP syntax doesn't allow named parameters in a function. Named parameters are very convenient over positional parameters because:

  1. You don't need to remember the position of each parameters when you use a function.
  2. A function with a lot of parameters is more readable when you can use named parameters.
  3. If you adding more parameters to a function over time, you have to be very careful not to mixup the position of the parameters. This lead to less readability.

Here's an example on how akward positional parameters can be. Suppose you have this function:

function grabUrl ($urlToGrab, $filePathToSavePage, $errorGettingUrl, $errorSavingPage)

The order of the parameters make sense, you put at the beginning information (the url to grab, the path to file to save a web page) and then, at the end, the errors (the error to display if the url is unreachable, the error to display if you can't save a page).

Now, let say you use this function all over the place. And then, you want to add a new parameter to ping the url before you try grab it.

function grabUrl ($urlToGrab, $filePathToSavePage, $errorGettingUrl, $errorSavingPage, $usePing)

Now the logic of the positional parameters are less clear, especially if you start to add more new parameters.

I'm working on a PHP project these days, and I'm constantly augmenting the parameters of older functions because I add new functionalities and try to generalize more their use (what some call "refactoring").

Clearly, I need named parameters, but since its PHP, how I will do that?

I asked the question on NYPHP-Talk mailing list and had good pointers.

The first thing to do is to pass an associative array to the function.

Then depending of the way you want to use your function, you can use either extract () or array_merge ().

I should note right away that my solution is inspired by the Recipe 6.4 ("Using Named Parameters") of the excellent PHP Cookbook (O'Reilly). If you don't have the book, you can download the recipe I'm talking about.

In both example, we want to create a simple RSS 2.0 item.

First example: using extract ()

function formatItem ( $args ) {
   $argsDefault = array (
           'title' => date ('r'),
           'link' => "http:\//blog.scriptdigital.com",
           'description' => "Whatever...",
           'category' =>  "Out of the box");

     extract ($argsDefault, EXTR_PREFIX_ALL, 'arg');
     extract ($args, EXTR_PREFIX_ALL, 'arg');

     $item = "";
     $item .= "<title>$arg_title</title>\n";
     $item .= "<link>$arg_link</link>\n";
     $item .= "<description>$arg_description</description>\n";
     $item .= "<category>$arg_category</category>\n";

     return $item;

}

$rssItem = array (
       'link' => "http:\//apple.com",
       'description' => "Apple web site.");

$rssItem = formatItem ( $rssItem );
echo $rssItem;

-->
<title>Sat, 10 May 2003 14:19:42 -0400</title>
<link>http://apple.com</link>
<description>Apple web site.</description>
<category>Out of the box</category>

Here, using an associative array show clearly what are the params when we call the function ($rssItem), and its also clear what are the params in the function ($argsDefault). What we are doing is overwriting the default values with the second call to extract () and then append 'arg_' to the name of the variable passed to the function, so it will be more readable (we will know immediately that these variable were passed as parameters to the function). Also, if the function doesn't provide all parameters, we can use default values for the parameters that were not passed to the function without having the function causing an error.

This function is useful if all you want is to return something new, but not if you want to return the arguments of the function with the default parameters and their values added to the original arguments of the function. For that, its probably better to use the next function that use array_merge ().

Second example: using array_merge ()

function formatItem ( $args ) {
   $argsDefault = array (
           'title' => date ('r'),
           'link' => "http://blog.scriptdigital.com",
           'description' => "Whatever...",
           'category' =>  "Out of the box");

     $args = array_merge ($argsDefault, $args);
     return $args;
}

$item = array (
       'link' => "http://apple.com",
       'description' => "Apple web site.");

$cleanItem = formatItem ( $item );

$xmlItem = "";
foreach ($cleanItem as $xmltag => $val) {
   $xmlItem .= "<$xmltag>$val</$xmltag>\n";
}

echo $xmlItem;

-->
<title>Sat, 10 May 2003 15:02:39 -0400</title>
<link>http://apple.com</link>
<description>Apple web site.</description>
<category>Out of the box</category>

In this example, array_merge () will overwrite the missing parameters in $args with the default parameters ($argsDefault). Its probably more convenient this way. And if you need to use directly the parameters in the function, it will be also very readable because the name of the array tell you that the values that it holds are coming from the values passed to the function (for example: args['description']).

comments: 0

1:10PM EDT [ /PHP | # ]

Tue 18 Feb 2003

myphp

David Sklar, one of the author of the excellent PHP Cookbook (but do check the errata page) has released myphp, a nifty extension for MySQL that allow stored procedures written in PHP.

As the author put it: myphp.so implements a MySQL UDF that interprets PHP code.

Here's an example from the author:

mysql> CREATE TABLE test (s varchar(255));
mysql> insert into test values ('iguana'),('turtle'),('aardvark');
mysql> select s,php('strlen()',s) FROM test;
+----------+---------------------------+
| s        | php('strlen()',s) |
+----------+---------------------------+
| iguana   | 6                         |
| turtle   | 6                         |
| aardvark | 8                         |
+----------+---------------------------+
3 rows in set (1.07 sec) 

More: http://www.sklar.com/page/article/myphp [via PHPDeveloper.org]

comments: 0

8:07PM EST [ /PHP | # ]

Fri 14 Feb 2003

PHPosxom Mailing List

Robert Daeley:

PHPosxom Mailing List
I have set up a PHPosxom mailing list for user discussion about all things PHPosxom. :) To subscribe, send a blank email to: phposxom-subscribe [at] yahoogroups [dot] com or visit the group homepage at Yahoo groups. [via Celsius1414]

comments: 0

11:39AM EST [ /PHP/PHPosxom | # ]

Thu 13 Feb 2003

IBM DeveloperWorks: Writing Efficient PHP

[Via PHPDeveloper.org]

From About this Tutorial

This tutorial targets the PHP developer who already understands PHP, but wants to write more efficient PHP code or to improve the performance of existing PHP applications. It is not intended as an introduction to PHP -- plenty of other resources are available for that. Rather, it assumes that you already have an installation of PHP available and are familiar with the basic PHP syntax.

In this tutorial, you will learn ways to improve the performance of your PHP code. The tutorial is in four main parts:

IBM DeveloperWorks: Writing Efficient PHP
(free registration required)

More: https://www6.software.ibm.com/developerworks/education/wa-effphp/

comments: 0

4:17PM EST [ /PHP | # ]

Tue 11 Feb 2003

Looking for a PHP class or simple application for threading message

I'm looking either for a class or a PHP application for threading messages like what you see on a bulletin board.

What I'm looking for is something basic and lightweight and portable (Unix, Windows) and run on MySQL. I don't need an interface for access control or a lot of bells and whistles. But if it come with access control, that's ok too if its still lightweight. Ideally, it should update an RSS file when new messages are posted.

When I say something basic, I'm thinking of a really bare interface, like the one you see on some of the sites generated by Manila from UserLand. Here's a good example:
http://backend.userland.com/discuss/msgReader$112?mode=day
(Disclaimer: I'm not saying here that Manila is basic, on the contrary its a wonderful piece of software, I'm just showing here an example of what I'm looking for, and Manila can generate much more richer interfaces for its discussion group.)

comments: 0

5:02PM EST [ /PHP | # ]

Mon 10 Feb 2003

PHPosxom 0.6b2 Known Bugs

Robert Daeley have posted a list of the know bugs of PHPosxom 0.6b2.

PHPosxom 0.6b2 Known Bugs
Thanks to everyone who has downloaded 0.6b2 and sent in feedback. So far, things seem to be going pretty smoothly. There are three known bugs at this point that some folks are seeing; you may or may not see these, depending on what combination of features you have turned on/off. These will be fixed in the next beta release, 0.6b3, which will be out the door this week.

[via Celsius1414]

comments: 0

3:58PM EST [ /PHP | # ]

Sat 08 Feb 2003

Conférence PHP Québec 2003 - (Montréal, March 20 & 21rst, 2003)

Wow, Montréal is my hometown!

Conférence PHP Québec 2003 - (Montréal, March 20 & 21rst, 2003)
The PHP Québec association announces the Conférence PHP Québec 2003. The conference will take place in the École Polytechnique de Montréal, Québec, Canada. The Conférence PHP Québec features two days of conferences, with outstanding customer cases from Canada, and cutting edge technical sessions, hosted by international experts. An exhibitor room will showroom professional solutions. Learn more about those exciting days at phpconf.phpquebec.com.
[via PHP: Hypertext Preprocessor]

More: French
More: English

Featuring:

comments: 0

1:37AM EST [ /PHP | # ]

Introduction to Unix Permissions

On O'Reilly PHP DevCenter, a short and nice general introduction on Unix permissions [via PHPDevelopper.org]. Doesn't cover PHP yet on this question. Working with Permissions in PHP, Part 1 by John Coggeshall

comments: 0

12:10AM EST [ /PHP | # ]

Fri 07 Feb 2003

PHPosxom 0.6b2

Robert Daeley have released PHPosxom 0.6b2. I hacked PHPosxom 0.5d to run this blog, and I must say that it was a lot of fun. The code is clean and well done. And its fun to see in PHP someone naming its variables with innerCasing :). Now, I'm just waiting to be slashdoted to see how the whole thing will hold...

Also, don't miss Robert's instructions for posting from NetNewsWire to Blosxom and PHPosxom.

comments: 0

1:58AM EST [ /PHP | # ]