Extract first 100 characters from Mysql field

I have looked about and found plenty of information about getting the
first 100 characters from a mysql field, which is what i want. But, is
there a way to first remove any html that may be in the field first, so
that only the actual words are included.
I have a news site and at the moment there is a separate summary field,
which really would be nice to get rid of, but since the main field has
html put into it (i was KTML 4 as the RTE) when i try and select the
first 100 characters it often ends up being very short, with unclosed
html tags. So what would be great is a way to strip it of html then
select the first 100 characters.
chris.huh [ Mo, 22 Januar 2007 12:11 ] [ ID #1604185 ]

Re: Extract first 100 characters from Mysql field

chris.huh wrote:
> I have looked about and found plenty of information about getting the
> first 100 characters from a mysql field, which is what i want. But, is
> there a way to first remove any html that may be in the field first, so
> that only the actual words are included.

I guess it's easies to fetch the whole field to php and use ereg* functions to
strip away the HTML tags and then take the 100 first characters.


--

//Aho
Shion [ Mo, 22 Januar 2007 12:17 ] [ ID #1604187 ]

Re: Extract first 100 characters from Mysql field

J.O. Aho wrote:

> chris.huh wrote:
> > I have looked about and found plenty of information about getting the
> > first 100 characters from a mysql field, which is what i want. But, is
> > there a way to first remove any html that may be in the field first, so
> > that only the actual words are included.
>
> I guess it's easies to fetch the whole field to php and use ereg* functions to
> strip away the HTML tags and then take the 100 first characters.
>
>
> --
>
> //Aho

So with ereg* you specify what things it should search for in a
variable and then it removes them leaving just what is left in another
variable. Then i apply the 'select first 100 characters; function to
that. Is that right?
chris.huh [ Mo, 22 Januar 2007 12:44 ] [ ID #1604188 ]

Re: Extract first 100 characters from Mysql field

chris.huh wrote:

> So with ereg* you specify what things it should search for in a
> variable and then it removes them leaving just what is left in another
> variable. Then i apply the 'select first 100 characters; function to
> that. Is that right?

Seemed it was even easier than using ereg*

$stringnotags=substr(strip_tags($stringwithhtml),0,100);


http://www.php.net/strip_tags
http://www.php.net/manual/en/function.substr.php

--

//Aho
Shion [ Mo, 22 Januar 2007 12:50 ] [ ID #1604189 ]

Re: Extract first 100 characters from Mysql field

J.O. Aho wrote:

> chris.huh wrote:
>
> > So with ereg* you specify what things it should search for in a
> > variable and then it removes them leaving just what is left in another
> > variable. Then i apply the 'select first 100 characters; function to
> > that. Is that right?
>
> Seemed it was even easier than using ereg*
>
> $stringnotags=substr(strip_tags($stringwithhtml),0,100);
>
>
> http://www.php.net/strip_tags
> http://www.php.net/manual/en/function.substr.php
>
> --
>
> //Aho

Ah brilliant - exactly what i needed, and one small line. Perfect,
thanks Aho.
chris.huh [ Mo, 22 Januar 2007 12:58 ] [ ID #1604190 ]
PHP » alt.php » Extract first 100 characters from Mysql field

Vorheriges Thema: sql string + variable
Nächstes Thema: problem execution order