MW

Tag php - page 4

Recent Posts

Redaxo Impressions (March 13, 2008)

Today at work I had my first contact with Redaxo, a German contact management system written in PHP and based on MySQL. I’ve heard of it before but never used it, there always seemed to exist better solutions - Drupal for example. But the company I work for already committed itself to Redaxo for this client. So no chance to work with Drupal this time - a pity.

But new experience with other solutions is a great thing. I thought Redaxo had its right to exist: its very small.

Here I don’t mean the filesize or whatever - I mean the size of the admin panel to begin with. It has not that many features. It’s very easy to understand. The average John Doe which is not that websavvy wont be confused by a multitude of choices. And I think I got to know pretty much all of Redaxo in just a few hours — which I can’t say about the steep learning curve of Drupal. And since the website of this client will be small I first thought Redaxo will do quite good.

The deeper I dug into Redaxo the more annoyed I got by its shortcomings though: There are modules and addons with which you can do pretty much all you need. But not one module I used was ready out of the box. Always I had to do adjustments. Drupal does much better here. And what you can do with overloading in Drupal is millions of light years ahead of what Redaxo is able to do. Oh and the developers of Redaxo really should take a look at Drupals documentation! After some searching on the official website I stuck to grep -R "function XXX" . for Api references…

continue reading...

Drupal Spam module ported to D6 (March 02, 2008)

I’ve got a pretty well working spam module for Drupal 6 now. Please test it, you can get the most up-to-date tarball here:

http://drupal.org/node/222849

Take the last tarball available, should be the best. I’m planning on testing it live on this site. On my localhost testbox it worked pretty well.

continue reading...

Second Markdownify Beta released (February 03, 2008)

I’ve just released a second Markdownify Beta with better PHP 4 support and some other small bug fixes. You can download it from sourceforge.

continue reading...

Markdownify Beta released (February 03, 2008)

Finally I’ve completed the Markdownify website. Also I’ve released the first beta, here the news text from SourceForge:

This is the first beta release of Markdownify - the HTML to Markdown converter for PHP.

It is very stable and should handle nearly all features of Markdown and Markdown Extra syntax. Missing are only two things:

  • “Markdown inside block elements” for Markdownify Extra
  • word wrapping

These two things will be added before the first “stable” release. Additionally some performance improvements will hopefully be added.

You are encouraged to use this release in your web applications. Please let me know if you find any bugs. Also a code review by anyone would be very much appreciated!

Download it now

continue reading...

Close HTML Tags (June 16, 2007)

If you cut a html-formatted string at some random position (e.g. with my truncate() function) you might mess up the html. To circumvent that, this function will close all open tags at the end of the string:

The original function was written by connum at DONOTSPAMME dot googlemail dot com

    /**
     * close all open xhtml tags at the end of the string
     *
     * @param string $html
     * @return string
     * @author Milian Wolff <mail@milianw.de>
     */
    function closetags($html) {
      #put all opened tags into an array
      preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
      $openedtags = $result[1];
     
      #put all closed tags into an array
      preg_match_all('#</([a-z]+)>#iU', $html, $result);
      $closedtags = $result[1];
      $len_opened = count($openedtags);
      # all tags are closed
      if (count($closedtags) == $len_opened) {
        return $html;
      }
      $openedtags = array_reverse($openedtags);
      # close tags
      for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)){
          $html .= '</'.$openedtags[$i].'>';
        } else {
          unset($closedtags[array_search($openedtags[$i], $closedtags)]);
        }
      }
      return $html;
    }

continue reading...

html2text rewrite (May 16, 2007)

A few days ago I started a complete rewrite of html2text. It now uses a new htmlparser (also written by me) which should make the whole HTML cleanup process obsolete. The generic XML parser which is currently used dies on invalid XHTML, with my parser it should be possible do handle errors and parse HTML 4.01 documents without any regex magic beforehand.

You’ll hear more of this in about a week as I’ll be on vacation until the 24th.

continue reading...

UTF-8 Wordwrap (May 05, 2007)

If you use UTF-8 in your PHP projects you may want to use [wordwrap](http://www.php.net/wordwrap)(). But that function can’t handle multibyte characters and may mess up your text.

Don’t be annoyed - help is near!

The only PHP UTF-8 wordwrap function I found was the one by tjomi4 at yeap dot lv in the notes of the PHP manual. I took it and improved it a bit:

  1. completly the same syntax as the original wordwrap function: string utf8_wordwrap(string $str, integer $width, string $break [, bool $cut]);
  2. The $cut parameter is supported (tjomi4’s function only supports $cut = true).
    But be careful : I use regular expression word boundaries (\b) for this feature. I’m not sure if this works everywhere!

  3. The function uses the multibyte extension if installed for counting the string length
  4. The regular expression inside the while loop is shorter and uses [preg_match](http://www.php.net/preg_match)() instead of [preg_replace](http://www.php.net/preg_replace)(). That should improve performance and prevent a strange bug (Compilation failed: regular expression too large)

continue reading...

html2text.php version 1.3 released (December 24, 2006)

Update: Use Markdownify, it’s the successor to html2text.

I just released html2text version 1.3 which sports a ton of bug fixes. Most notably all features of php markdown extra are now fully supported, including footnotes and abbrevations.

Also wrapping should work like intended and inline links (like <foo@bar.com>) won’t be converted to block links (like [foo@bar.com]([foo@bar.com](mailto:foo@bar.com))).

In the next version I’ll add some more options, especially disabling php markdown extra support. Also I’ll clean up the code a bit.

Merry Christmas to you!

continue reading...

html2text.php 1.1 (July 23, 2006)

Update: Use Markdownify, it’s the successor to html2text.

I changed my html2text.php function and it now supports non markdownable elements better. Previously something like <p class="foobar">...</p> would have resulted in <p>...</p>. Now these elements (which could be ported to markdown) will be left in plain html.

Additionally I made some changes which should lead to an improved performance.

Download

Get it while it’s hot: html2text.php 1.1 (.tar.gz ~ 120.9 KB)

Known Bugs

Yes, there are some, which I’ll try to fix in the next days (note: to better point out the bugs I just write what happens if you convert html to markdown to html):

  • Also if the parent element (e.g. <table>) gets parsed and a child <tr>,<td> or<th> has attributes they will be ignored and dropped. Workaround: Add a attribute to the parent element (e.g. a class / id).
  • If you give a single <li> element in the middle of a list some attributes it wont lose them, but will produce not well formed html:

    <ul><li>abc</li> <li class="foo">bar</li> </ul>
    

continue reading...

String Truncate (July 19, 2006)

If you write a CMS you will have to truncate your contents to automagically create summaries. The following function will do the job:

If you write a given string (default '<!--MORE-->') inside one of your contents, the text will be truncated to that position.

Else the function looks for the nearest word boundary after at least $len characters and cuts there. Because that might be directly inside your text $append will be appended. To prevent that the markup is messed up, closetags() is called.

    /**
     * if $splitter is found inside the $str, everything before $splitter will be
     * returned.
     * else truncates $str after $length chars (actually at the nearest
     * word boundary after at least $len characters). Also $append will be added to
     * $str
     *
     * @param string &$str
     * @param integer $length
     * @param optional string $hardbreak
     * @param optional string $append
     * @return string
     * @author Milian Wolff <mail@milianw.de>
     */
    function truncate($str, $len = 200, $splitter = '<!--MORE-->', $append = '…') {
        if (strlen($str) <= $len) {
            return $str;
        }
        if ($len > 0 && !strstr($str, $splitter)) {
            preg_match('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$len.',}\b#U', $str, $matches);
            $str = $matches[0];
            # remove trailing opener tags and close all other open tags:
            $str = closetags(preg_replace('#\s*<[^>]+>?\s*$#', '', $str).$append);
        } else {
            $arr = explode($splitter, $str, 2);
            $str = $arr[0];
        }
        return $str;
    }

continue reading...