473,414 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Limiting HTML char count code with php.

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
5 2439
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
> >
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
11
by: Michael B Allen | last post by:
Here's fragment of C from an allocator. This allocator permits the user to specify a callback to reclaim memory if necessary. If memory cannot be found the 'if (reclaim)' block is entered and the...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
9
by: | last post by:
void show( char *s, ...) is a function seemd like prinf code -------------- #include <stdio.h> #include <stdarg.h> void show( char *s, ...) { va_list stage;
6
by: vaidehikedlaya | last post by:
Hello, I am using gmp.h library. I am trying to put mpz_t variable into char buffer and back. I came across mpz_import/mpz_export to suffice this purpose. But, I am not very clear about using...
10
by: vignesh4u | last post by:
I am trying to implement the Split function in C ie. if i have a string: char* S="This is a test"; and if i try to split based on space ' ' it should return an array of strings like: ...
11
by: icarus | last post by:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to Kelvin). Using gcc 4.0. The user is supposed to enter q or any other non-character to exit the program. Problem with this...
2
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string...
2
by: rx25c | last post by:
Hi. I'm trying to limit the output of a for loop to 200 counts and then breaking it. The purpose of the loop is to print out defined unicode characters onto a html table after a user sets the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.