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.

About these ads

Actions

Information

8 responses

15 04 2009
Golchi

This sounds cool. I’ll definitely check this :)

7 06 2009
strlen vs mb_strlen function in php « Flame’s blog

[...] 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 [...]

20 07 2009
htmlcutstring python package « Flame’s blog

[...] 20 07 2009 I released php, javascript implementation for cut html string, these programs cut the html string by keeping html tags as it is. Now I released same in python with the name htmlcutstring. Check this at [...]

21 08 2009
Krishna

It’s very nice plug-in. I have used it for php. But I got problem with ” “. If there is ” ” in html string then it’s showing domdocucment warning for wrong entity. ” ” is not valid entity for it. Let me know, if you have any idea for the same. Other solution is i have to replace   with space or something else to get correct cut html string.

Thanks

21 08 2009
Krishna

it’s replacing ‘nbsp’ in my previous comment as i have written the html code of nbsp…hope you understood it…:)

21 08 2009
Prajwala

Hi krishna,

I am glad that cut html string module helped you. I will check this &nbsp issue.

16 09 2009
Bob

Hmm… for some reason this class will not output a single character, even in the simplest implementation. I’ve looked inside the class. All strings are stripped to zero length. It just won’t work for me. Even this won’t output:

$outPut = new HtmlCutString(“Hello There Everybody”,10);
print $outPut->cut();

That’s just a plain text string. It won’t work either.

16 09 2009
Prajwala

it is working for me, I tested with the string you specified “Hello There Everybody”. I am using php5.2.4 version. May be it is problem with php version. What is the php version you are using?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: