Current location: Hot Scripts Forums » Programming Languages » PHP » substr() question

substr() question

Reply
  #1  
Old 06-29-09, 05:31 AM
jonnekke jonnekke is offline
Junior Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 642
Thanks: 0
Thanked 0 Times in 0 Posts
substr() question

Hi there..

I got a part of my website where I want to display a preview of a long story.

For example the first 40 words should be there, but a sentence should not be "broken"
So the maximum should be 40 words.

I can substr() to a number of characters.. but how to manage my problem as told above?

_j
Reply With Quote
  #2  
Old 06-29-09, 06:31 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 20 Times in 18 Posts
explode() the string on all white spaces.
array_splice() it to get the first 40 words.
implode() the space again.

Last edited by Nico; 06-29-09 at 06:33 AM.
Reply With Quote
  #3  
Old 06-29-09, 06:45 AM
jonnekke jonnekke is offline
Junior Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 642
Thanks: 0
Thanked 0 Times in 0 Posts
thnx!..

Got it fixed with the split at the last "." in the text:

PHP Code:
$item_rechterkant_verhaal explode(' '$item_rechterkant_verhaal); 
array_splice($item_rechterkant_verhaal40); 
$item_rechterkant_verhaal implode(' '$item_rechterkant_verhaal);

$item_rechterkant_verhaal explode('.'$item_rechterkant_verhaal);
$aantal count($item_rechterkant_verhaal)-1;
array_splice($item_rechterkant_verhaal$aantal);
$item_rechterkant_verhaal implode(' '$item_rechterkant_verhaal)."."

Last edited by jonnekke; 06-29-09 at 07:04 AM.
Reply With Quote
  #4  
Old 07-06-09, 04:56 AM
jonnekke jonnekke is offline
Junior Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 642
Thanks: 0
Thanked 0 Times in 0 Posts
I got this fixed with a "." as separator.
But there is a problem if a sentence end with a "!" or a "?"
How can I build in this multiple separator?.

_j
Reply With Quote
  #5  
Old 07-06-09, 05:45 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 20 Times in 18 Posts
Use preg_split() instead.

PHP: preg_split - Manual
Reply With Quote
  #6  
Old 07-06-09, 06:01 AM
jonnekke jonnekke is offline
Junior Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 642
Thanks: 0
Thanked 0 Times in 0 Posts
okay...

so it should become something like this:

PHP Code:
$news preg_split("\.|\?|\!"$news);                                    
$aantal count($news)-1;
array_splice($news$aantal);
$news implode('.'$news)."."
to split on the sentence ending on: . - ! - ?
Reply With Quote
  #7  
Old 07-06-09, 06:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 20 Times in 18 Posts
Unlike the ereg_* functions, the preg_* ones work with delimiters (the expressions begin, and end with the same character). See the examples on the page I posted.
Reply With Quote
  #8  
Old 07-06-09, 06:17 AM
jonnekke jonnekke is offline
Junior Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 642
Thanks: 0
Thanked 0 Times in 0 Posts
cool! fixed it.. thnx nico!

PHP Code:
$news_1 preg_split("/\.|\?|\!/"$news, -1PREG_SPLIT_OFFSET_CAPTURE);
                                    
$news_intro $news_1[0][0];                                
$from = ($news_1[1][1])-1;                                
                                    
$news_intro_end substr($news$from1) ; 
Reply With Quote
  #9  
Old 07-06-09, 06:24 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 20 Times in 18 Posts
Nice one!
Reply With Quote
  #10  
Old 07-06-09, 06:25 AM
jonnekke jonnekke is offline
Junior Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 642
Thanks: 0
Thanked 0 Times in 0 Posts
EDIT: Sorry. to fast. it's an array so for sure I can count it!

can I count the numbers of "founds" in this preg_split..?

Last edited by jonnekke; 07-06-09 at 06:26 AM. Reason: to fast
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie member, (semi)novice developer has a question WorkyMcWorkerson Script Requests 2 11-26-08 02:28 PM
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 03:04 PM
Posting a question / answer on site markcody PHP 2 11-23-04 02:58 PM
[PHP] Array question UmiSal Script Requests 1 04-05-04 02:52 PM
question and answer software jaydifox C/C++ 0 02-21-04 10:26 AM


All times are GMT -5. The time now is 07:30 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)