Emacs + php-mode in windows

19 11 2009
My development machine is having Ubuntu and I love to use Emacs than any other editor. Recently I need to work on windows.  I wrote a blog post about how to make Linux like environment in windows. I wanted to use Emacs in windows. To install Emacs in windows I followed the instructions as it is from http://www.claremontmckenna.edu/math/alee/emacs/emacs.html

I want to work on PHP, so I need a php-mode for Emacs, otherwise it will be very difficult. I tried a lot to install php-mode in windows as specified at https://prajwalaa.wordpress.com/2009/03/19/using-emacs-to-edit-php-files/ but I was getting “error: `c-lang-defconst’ must be used in a file” error message. Finally after searching for long time I figured out how to enable php-mode.

I downloaded php-mode and extracted and placed in my C:\emacs directory. And added following lines in my C:\emacs\.emacs.d\init.el file


(add-to-list 'load-path "C:\emacs\php-mode")
;;(load 'php-mode)
(autoload 'php-mode "php-mode" "Major mode for editing php code." t)
(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
(add-to-list 'auto-mode-alist '("\\.php$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))

After adding above lines and save. I restarted emacs. Then Php-mode is working fine. Hope this will help others to install Emacs and enable php-mode.





strlen vs mb_strlen function in php

7 06 2009

I wrote a simple class in php called cut html string . This class with cut the html string without considering html tags but the output will have the html tags as it is. Here I used strlen function to find the length of string. This function return the number of bytes of the string. If string has some utf8 special characters then it returns wrong value, because those special characters will take more bytes. I want to find the number of characters in the string. mb_strlen function returns the number of characters in string. A multi-byte character is counted as 1. I prefer to use mb_strlen function to find the character count of a string.





Facebook Live Stream Module for Drupal Released

29 05 2009

We’ve just released a module. This module leverages the Facebook Open Stream API. Once installed, the module allows users of the Drupal instance to access their Facebook account and view the real time stream of their friends’ updates from Facebook in a block within their Drupal instance. Users can control the number of posts viewable via the profile settings tab.

Check http://www.azrisolutions.com/journal for more information.





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.