General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 21st, 2002, 03:19 PM
arnorg arnorg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 13 arnorg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
substr() not i a midde of some word!

Hello i want to cut a string to show a preview of the news in the frontpage..

but if i substr the first 250 letters, it may end up in a middle of word and i donīt want that?

Reply With Quote
  #2  
Old October 21st, 2002, 03:33 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
just appened ... to the end of whatever you substr out. for example
PHP Code:
echo substr($strVar,0,250) .'....'

Reply With Quote
  #3  
Old October 21st, 2002, 05:55 PM
arnorg arnorg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 13 arnorg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
this doesnīt work..

You donīt know what iīm talking about...

iīm not talking about splicing a "...." at the end of the string...

I donīt want to end in a middle of word if that is the 250 letterplace..

i wanīt to get some method to overcome that the preview ends in a middle of some word..

Reply With Quote
  #4  
Old October 21st, 2002, 08:15 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
How about something like this:

<?php

define("MAX_LENGTH", 50);

$news = "today marks the launch of devscripts.com, a site where developers can find free programming scripts and source code, rate scripts and more!";

$arrNews = explode(" ", $news);
$len = 0;
$output = "";

for($i = 0; $i < sizeof($arrNews) || $len >= MAX_LENGTH; $i++)
{
$len += strlen($arrNews[$i]);
$output .= $arrNews[$i];
}

echo $output;

?>

Some code like that shown above would split each word into an array and grab the words until the length of the output string was equal to or just greater than 250... maybe you could work around some code like this?

Reply With Quote
  #5  
Old October 21st, 2002, 09:13 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 7
PHP Code:
 $data substr$string'0''250' );
$chunks explode' ',  $data );
array_pop$chunks );
$text implode' '$chunks ); 

That should solve your problem.....

PS: Untested but should work.......

Reply With Quote
  #6  
Old October 22nd, 2002, 09:15 AM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
hm yeah jpenn that will work i was just going for something simple, google style =D
this is your resu.....
heh

Reply With Quote
  #7  
Old October 22nd, 2002, 09:27 AM
arnorg arnorg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 13 arnorg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think you donīt know much in php programming...

Reply With Quote
  #8  
Old October 22nd, 2002, 09:38 AM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
lol sure thing arnorg. try to keep it nice, i didn't make any negative comments about the fact you needed help with this =(

Reply With Quote
  #9  
Old October 22nd, 2002, 12:43 PM
arnorg arnorg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 13 arnorg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thatīs true

but although tou wrote some crap that maked no sense... with what i was asking about..

Reply With Quote
  #10  
Old October 22nd, 2002, 12:44 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Of course it made sense. Simply adding '...' at the end of the selected phrase, be it in the middle of a word at between words, clearly indicates that there is more to read. That's what the ellipsus means. It's certainly not the BEST solution, but it's an adequate and lazy one

And as I said jpenns' was mucho bettar!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > substr() not i a midde of some word!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT