[Solved] chopping of text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[Solved] chopping of text

Post by ol4pr0 »

Oke . small question basicly.

If i have like a 30 line text but on the front page i only want to show lets say like 10 % of it and with a link read more ....

How to do that.. do i use a count word function or is there some other fancy way for doing this.
Last edited by ol4pr0 on Sun Aug 29, 2004 11:28 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

substr can be used.. unless you don't want to come into the middle of a word.. preg_replace can be used for extracing the words and stripping/ignoring tags and bbcode
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

thx
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

[quote=feyd]
substr can be used.. unless you don't want to come into the middle of a word
[quote]

I am prolly doing something wrong here. but i am still ending up in the middle of some word

Code: Select all

echo substr($string, 0,30);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like this may work (untested)

Code: Select all

<?php

preg_match('#^\s*(.{26,}?)\s*.*$#', $test, $match);

?>
it should grab the last word, if it runs over.. not too sure though.
Last edited by feyd on Thu Aug 26, 2004 4:33 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i wonder whats wrong with his substr? it looks good to me.

can u echo out the $string and get the text u wish to play with?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Oke just some stupid text i put in ..

text = Jack went to the store to get himself a large bottle of whiskey

Code: Select all

echo substr($file_content, 0,36);
output
Jack went to the store to get himsel
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

yeah its doing the job

0 is where to start, 36 is how many chars over (including white spaces) it will capture.

if u count, himself, the l in that makes for 36 chars

=]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

$text = 'Jack went to the store to get himself a large bottle of whiskey';

preg_match('#^\s*(.{26,}?)\s+.*$#', $text, $match);

var_export($match);

?>
outputs

Code: Select all

array (
0 => 'Jack went to the store to get himself a large bottle of whiskey',
1 => 'Jack went to the store to get',
)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Thanks. altho feyd i do not completely understand what u are doing in ure example
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it grabs a minimum of 26 characters, if the 27th character isn't a space, it continues out until there is a space. Thus, not chopping words in two.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

There is some problem with this tho..

Code: Select all

echo $row['message']; // does echo out the message ( but compleet )
		preg_match('#^\s*(.{60,}?)\s+.*$#', $row['message'], $match);
		echo '<font size="-1">';
		echo($match[1]); // this does nothing var_export($match) prints out Array ( )
Well actually now its doing this.
array (

0 => 'Usted puede ahora enviar su currículum a nuestra página, de esta manera las Empresas podrán revisar su CV. Pero usted tendrá que esperar hasta que el diseño y codificación de las páginas de la empresa estén listos.

Usted podrá ingresar su CV en nuestra Base de Datos pero todavía no podrá utilizar el Interface Administrativo que es el que le permitirá cambiar datos, fotos en su currículum, ya que aún no está terminado. ',

1 => 'Usted puede ahora enviar su currículum a nuestra página, de esta manera las Empresas podrán revisar su CV. Pero usted tendrá que esperar hasta que el diseño y codificación de las páginas de la empresa estén listos.

Usted podrá ingresar su CV en nuestra Base de Datos pero todavía no podrá utilizar el Interface Administrativo que es el que le permitirá cambiar datos, fotos en su currículum, ya que aún no está terminado.',

)
As you can see the both arrays are the same
Last edited by ol4pr0 on Sun Aug 29, 2004 11:15 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to add the 's' modifier into the regex:

Code: Select all

//.........
preg_match('#^\s*(.{60,}?)\s+.*$#s', ...........
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

that s would stand for space ? i asume
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply