Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2004, 06:45 AM   PM User | #1
mat
Regular Coder

 
Join Date: Jul 2002
Posts: 199
Thanks: 0
Thanked 0 Times in 0 Posts
mat is an unknown quantity at this point
text - cut off at the nearest space

To grab only the first 150 characters from a mySQL table field containing text I use the following:

PHP Code:
Left(story,115
.. in my SQL select statement.

Unfortunately this tends to cut out in the middle of words though which isn't to great.


Is there a simple-ish using php I make it cut off at the nearest space?
mat is offline   Reply With Quote
Old 01-14-2004, 07:02 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
if you wanted to grab say the first $x words you could use MySQL's SUBSTRING_INDEX...

e.g. first 20 words ...
SELECT SUBSTRING_INDEX( fieldname ,' ', 20 ) ;

in PHP would probably be messy compared to the above ... something like ??

(untested and cheesy :: might give you a string between 150 - 164 chars)
PHP Code:
<?php
$pre 
substr$str ,148 );
/*assuming most words are less than 15 chars*/
$ext substr$str 149 15 ); 
$bits explode' ' $ext ) ;

echo 
$pre $bits[0];
?>
though perhaps there is some funky regex to do the same ?
MySQL would still be the best bet from a performace perspective
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 01-14-2004, 07:18 AM   PM User | #3
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
errr actually ignore that

this does work & gives a max of 150 chars

PHP Code:
<?php
$pos 
strrpossubstr$str 150 ) ,' ' ) ;
echo 
substr$str $pos );
?>
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 01-14-2004, 09:26 AM   PM User | #4
mat
Regular Coder

 
Join Date: Jul 2002
Posts: 199
Thanks: 0
Thanked 0 Times in 0 Posts
mat is an unknown quantity at this point
I see.. thank you.
mat is offline   Reply With Quote
Old 01-14-2004, 02:40 PM   PM User | #5
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
And I also wrote this for someone on here long ago which does basically the same thing except it parses the string and allows you to remove imperfections and attempts to cut the string off at the nearest sentence but under the word limit.

PHP Code:

<form name="foo" method="post" action="<?php echo $PHP_SELF ?>">
  <textarea name="input" cols="50" rows="6" wrap="VIRTUAL" id="input"></textarea>
  <br>
  <input type="submit" name="Submit" value="Submit">
</form>
<?php

$input 
$_POST["input"];

function 
process_string($string) {

// Number of words to return.
$word_limit 5;

// Remove line breaks, double spaces, etc from the string
// You can add to this section to clean up the string
// before attempting to count the number of words.
$string str_replace("\n"," ",$string);
$string str_replace("  "," ",$string);

// Split the string into an array of words.
// It is set to split when a single space is found
$word_array explode(" "$string);

// Counts the number of elements in the array
// which should be about how many words were in the string
$num_of_words count($word_array);

// Trims the array of words.  Removes all words after
// what is specified in the $word_limit 
$word_array_trimmed array_splice ($word_array0$word_limit);

// Converts the array of words back into a string
$final_string implode(" ",$word_array_trimmed);

return 
$final_string;
}

echo 
"<br><br>" process_string($input);

?>
It determines sentences based on periods being at the end of the sentence however there are some situations that will break that logic such as periods being used other than at the end of a sentence.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:02 PM.


Advertisement
Log in to turn off these ads.