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


substr() question

Reply
  #1 (permalink)  
Old 06-29-09, 05:31 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-29-09, 06:31 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-29-09, 06:45 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-06-09, 04:56 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-06-09, 05:45 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Use preg_split() instead.

PHP: preg_split - Manual
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 07-06-09, 06:01 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
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: . - ! - ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 07-06-09, 06:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 07-06-09, 06:17 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
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) ; 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 07-06-09, 06:24 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Nice one!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 07-06-09, 06:25 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
Trackbacks are On
Pingbacks are On
Refbacks are On

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 08:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.