Cut html string by keeping html tags as it is

12 04 2009

With the use of WYSIWYG editor user can enter html content. While displaying teaser(short content) of the content on home page or dashboard it is hard to cut the string properly. Regarding this I did not see any available code. I need to do this in javascript and PHP. I implemented same logic in both javascript and php. I hope it would be helpful for others. The code for javascript and php is available at http://code.google.com/p/cut-html-string/

Using PHP code

You can download PHP code from this link
Download code and extract it. Include the cutstring.php file and use like this

$data = "<span style='color:green;'>aa<em>BB</em><ul><li>one</li><li>two</li><li>three</li></ul></span>";
$wanted_count = 10;
$cutstrObj = new HtmlCutString($data,$wanted_count);
$new_string = $cutstrObj->cut();
echo $new_string;

Update: We can directly use cut_html_string function

$data = "<span style='color:green;'>aa<em>BB</em><ul><li>one</li><li>two</li><li>three</li></ul></span>";
$wanted_count = 10;
$new_string = cut_html_string($data,$wanted_count);
echo $new_string;

Update 2: Above code will output the following string

<span style="color:green;">aa<em>BB</em><ul><li>one</li><li>two</li></ul></span>

Using Javascript code

You can downlod javascript code from this link
Download code and extract it. Include the cutstring.js file in your html file and use like this.

var data = '<span style="font-weight:bold">aa<em>BB</em></span><span id="1">Test <b>Cutstring</b></span> bb';
var wanted_count = 10;
var cutstrObj = new CutString(data,wanted_count);
var newStr = cutstrObj.cut();
console.log(newStr);

update: Now we can directly use cutHtmlString function

var data = '<span style="font-weight:bold">aa<em>BB</em></span><span id="1">Test <b>Cutstring</b></span> bb';
var wanted_count = 10;
var newStr = cutHtmlString(data, wanted_count);
console.log(newStr);

Update 2: Above code will log the following output

<span style="font-weight: bold;">aa<em>BB</em></span><span id="1">Test <strong>C</strong></span>

First I implemented it easily in javascript using DOM. But it took lot of time in PHP to implement same logic. Finally I implemented same logic using DOMDocument in php.

Advertisements




Happy coding php with xdebug

19 03 2009

Today I started working on drupal. I am reading about AHAH, then went to drupal development tools page, from there I stopped at xdebug.
I know about xdebug but I didn’t think it really works well. I did not find good tutorial before about how to use it.
I am working with php and python. I always used to think php is not having good debugging support like python. But xdebug is there to fullfil it.
I found good xdebug tutorial. xdebug is very good. It tells about how to print the stack trace of errors, local variables, global variables, parameter values. Different configuration settings how to display them. Now I am happy to code in php.
We can not debug the code without using any IDE. The tutorial explained by using Eclipse PDT.
It supports profiling and code coverage. It generates results which another tools can understand. The tutorial explained how to use those tools also. I hope xdebug tutorial is good tutorial to read.





Using Emacs to edit PHP files

19 03 2009

When we are programming on any language a good editor(emacs :)), lint (which gives syntax errors) and interactive debugger helps us to code fast.

I want to use emacs to edit php files. There are other IDEs which supports lint and debugger but I feel much comfort with emacs. So my idea is to find good php-mode for emacs and I want to find sytax errors in emacs itself. And I want to find good interactive debugger like python debugger.

I found php-mode and compiler for php but I could not find interactive debugger in the command line for php.

PHP-mode for emacs:

http://sourceforge.net/projects/php-mode/

Download php-mode from the above location. Extract the compressed file and copy php-mode.el to emacs lisp path.
Refer this link for emacs lisp path http://www.gnu.org/software/emacs/elisp/html_node/Library-Search.html.
or you can put this php-mode.el file in some directory and you can add this directory to lisp path. To add this new directory to lisp path add this line in the .emacs file.

(add-to-list 'load-path "~/path/to/new/directory/")

And add following lines to .emacs file to enable php-mode

(require 'php-mode)
;; To use abbrev-mode, add lines like this:
(add-hook 'php-mode-hook
'(lambda () (define-abbrev php-mode-abbrev-table "ex" "extends")))

Enable Tab indentation to 2:

The php-mode indentation for arrays is not good. It will indent like this

$a = array(
           'foo' => 'bar',
           'gaz' => 'gazonk',
          ) ;

If the associated array is having another associative array. then it will be like this

 

$a = array(
           'foo' => array(
                          'bar' => 'bar info'),
           'gaz' => 'gazonk',
          ) ;

If we are having more values like that then the code does not look good. It looks good if it will be like this.

$a = array(
  'foo' => array(
    'bar' => 'bar info'),
  'gaz' => 'gazonk',
) ;

To get view looks like that add following code in .emacs file

(defun clean-php-mode ()
(interactive)
(php-mode)
(setq c-basic-offset 2) ; 2 tabs indenting
(setq indent-tabs-mode nil)
(setq fill-column 78)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 'c-lineup-arglist-operators))
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values

Enable php mode for drupal module files:

To enable php-mode for drupal module file add these lines to .emacs file

(add-to-list 'auto-mode-alist '("/drupal.*\.\(php\|module\|inc\|test\|install\)$" . php-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\.info" . conf-windows-mode))

In place of /drupal you can put your drupal code base path. I put my drupal project at /var/www/ directory I replaced /drupal.* with /var/www/

PHP lint:

Hardly no body use php in the command line, so to use we need to install php-cli (php command line interface). Using php at the command line will make us to find errors easily and fast. We can find syntax error with the following command.
php -l &lt;php file name&gt;

To find more about php-cli option follow this link
http://www.php-cli.com/php-cli-options.shtml

I am using emacs so I can directly check syntax errors in emacs without running the above command on the command prompt. To do that add the following lines in the .emacs file.

;; run php lint when press f8 key
;; php lint
(defun phplint-thisfile ()
(interactive)
(compile (format "php -l %s" (buffer-file-name))))
(add-hook 'php-mode-hook
'(lambda ()
(local-set-key [f8] 'phplint-thisfile)))
;; end of php lint

To check syntax errors press “F8” function key on emacs editor, then currently opened php file will complie and show the syntax errors if any.

PHP Debugger: I tried to find good interactive debugger like python debugger. But I could not find it. So I might be developing one in the future.