Page 1 of 2

[Solved] chopping of text

Posted: Mon Aug 23, 2004 1:43 pm
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.

Posted: Mon Aug 23, 2004 2:14 pm
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

Posted: Mon Aug 23, 2004 4:29 pm
by ol4pr0
thx

Posted: Thu Aug 26, 2004 4:09 pm
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);

Posted: Thu Aug 26, 2004 4:27 pm
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.

Posted: Thu Aug 26, 2004 4:28 pm
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?

Posted: Thu Aug 26, 2004 4:33 pm
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

Posted: Thu Aug 26, 2004 4:39 pm
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

=]

Posted: Thu Aug 26, 2004 4:39 pm
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',
)

Posted: Thu Aug 26, 2004 4:52 pm
by ol4pr0
Thanks. altho feyd i do not completely understand what u are doing in ure example

Posted: Thu Aug 26, 2004 5:08 pm
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.

Posted: Sun Aug 29, 2004 11:09 am
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

Posted: Sun Aug 29, 2004 11:15 am
by feyd
you may want to add the 's' modifier into the regex:

Code: Select all

//.........
preg_match('#^\s*(.{60,}?)\s+.*$#s', ...........

Posted: Sun Aug 29, 2004 11:19 am
by ol4pr0
that s would stand for space ? i asume

Posted: Sun Aug 29, 2004 11:26 am
by feyd