By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

Limiting HTML char count code with php.

P: n/a
Hi,

I know how to limit the number of characters using substr,
(http://uk.php.net/manual/en/function.substr.php).
But when using html that is not going to work because of all the possible
tags, (anchor, image, table...etc).

for example, if I have some text...
$text = "<a href='somelonnnnnnngg'>this is a small text</a>";

I want to create a function to make sure that there is not more than 10
chars displayed.

but if I use

$text1 = substr($text, 0,10 );
i get $text1= "<a href='s";

where I would like to get

$text = "<a href='somelonnnnnnngg'>this is a </a>";
^^ only 10
chars.

How could the above be achieved?

Many thanks.

Sims


Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In message <c5************@ID-162430.news.uni-berlin.de>, Sims
<si*********@hotmail.com> writes
Hi,

I know how to limit the number of characters using substr,
(http://uk.php.net/manual/en/function.substr.php).
But when using html that is not going to work because of all the possible
tags, (anchor, image, table...etc).

for example, if I have some text...
$text = "<a href='somelonnnnnnngg'>this is a small text</a>";

I want to create a function to make sure that there is not more than 10
chars displayed.

but if I use

$text1 = substr($text, 0,10 );
i get $text1= "<a href='s";

where I would like to get

$text = "<a href='somelonnnnnnngg'>this is a </a>";
^^ only 10
chars.

How could the above be achieved?


In essence: Pick out the bit between the single quotes and check it's
length.

All the stuff you need is at:

http://www.php.net/manual/en/ref.strings.php
(see explode, strlen)

BTW what are you planning doing it it's too long?

--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #2

P: n/a
> >
How could the above be achieved?
In essence: Pick out the bit between the single quotes and check it's
length.


Sorry I am not sure I follow what you are saying.
I want the output text length to be limited not the tags.

All the stuff you need is at:

http://www.php.net/manual/en/ref.strings.php
(see explode, strlen)

BTW what are you planning doing it it's too long?
Well if I can limit the number of characters it is easier to display.
for example if I do not want a table to have more than 50 chars then I could
truncate them, but I only want to truncate characters that will be
displayed.
not the html tags that are not displayed.

In my example given I want to limit to 10 the number of characters
displayed,
but not the tags should be excluded.

$text = "<a href='somelonnnnnnngg'>this is a </a>";
^^ only 10
chars

--
Five Cats


Sims

Jul 17 '05 #3

P: n/a
Sims wrote:
Hi,

I know how to limit the number of characters using substr,
(http://uk.php.net/manual/en/function.substr.php).
But when using html that is not going to work because of all the possible
tags, (anchor, image, table...etc).

for example, if I have some text...
$text = "<a href='somelonnnnnnngg'>this is a small text</a>";

I want to create a function to make sure that there is not more than 10
chars displayed.

but if I use

$text1 = substr($text, 0,10 );
i get $text1= "<a href='s";

where I would like to get

$text = "<a href='somelonnnnnnngg'>this is a </a>";
^^ only 10
chars.

How could the above be achieved?


As the quote goes.... 'I wouldn't go there from here'.

Why are you parsing the finished href? Can't you trim the text before
building the href?

Alternatively use regular expressions to parse out the bit between the tags.

Jul 17 '05 #4

P: n/a

"Kevin Thorpe" <ke***@pricetrak.com> wrote in message
news:40***********************@news.easynet.co.uk. ..
Sims wrote:
Hi,

I know how to limit the number of characters using substr,
(http://uk.php.net/manual/en/function.substr.php).
But when using html that is not going to work because of all the possible tags, (anchor, image, table...etc).

for example, if I have some text...
$text = "<a href='somelonnnnnnngg'>this is a small text</a>";

I want to create a function to make sure that there is not more than 10
chars displayed.

but if I use

$text1 = substr($text, 0,10 );
i get $text1= "<a href='s";

where I would like to get

$text = "<a href='somelonnnnnnngg'>this is a </a>";
^^ only 10
chars.

How could the above be achieved?


As the quote goes.... 'I wouldn't go there from here'.

Why are you parsing the finished href? Can't you trim the text before
building the href?

Alternatively use regular expressions to parse out the bit between the

tags.

Or, use strip_tags().

Garp
Jul 17 '05 #5

P: n/a
Garp wrote:
Or, use strip_tags().


That function name is a misnomer; it ought to be renamed:
strip_everything_between_lessthan_and_greaterthan.

:-/ I'm one third serious.

--
Jock
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.