SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old August 9th, 2002, 03:55 AM
Probe Probe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 Probe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
strtok question

$word = strtok(" n", $quotes);
for ($i = 0; $i < 50; $i++) {
echo "$word";
$word = strtok(" n");
}
thats a script i have to only display the first 50 characters/words??? of a row in a database that has been given the name $quotes
this code was given to me by a friend and unfortunately it is not working. does anyone have any suggestions on how to improve this code or does someone know of a better way of doing what i want. thanks for the help!

Reply With Quote
  #2  
Old August 9th, 2002, 07:51 AM
siteworkspro.com siteworkspro.com is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 92 siteworkspro.com User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to siteworkspro.com Send a message via AIM to siteworkspro.com
RE: strtok question

Probe, just gotta love dat name

Reply With Quote
  #3  
Old August 9th, 2002, 03:11 PM
Probe Probe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 Probe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: strtok question

that did not work either

Reply With Quote
  #4  
Old August 9th, 2002, 05:13 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: strtok question

Try something else

php Code:
Original - php Code
  1.  
  2. // Parse $quotes for ([notspace][space or end]){up to 50 times}
  3. ereg( "(^([^ rnt]*([ rnt]|$)){0,50})", $quotes, $r );
  4.  
  5. // Print it
  6. Echo $r[1];


Can you say me if it worked ?

Reply With Quote
  #5  
Old August 9th, 2002, 10:59 PM
Probe Probe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 Probe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: strtok question

i am away from my home computer right now but i will report in once i get home

Reply With Quote
  #6  
Old August 9th, 2002, 11:18 PM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: RE: strtok question


Quote:
umm doesn't the {0,50} bit mean its a number from 0-50?


No, it means the pattern can occur between 0 and 50 times...

i.e. a simple phone number regexp might look like:
php Code:
Original - php Code
  1.  
  2. <?php
  3. $match='/(1-)?[d]{0,3}-[1-9]{1}[d]{2}-[d]{4}/';
  4. //that was off the top of my head, so it may not be correct.
  5. ?>


Cheers,
Keith.

Reply With Quote
  #7  
Old August 10th, 2002, 12:02 AM
Probe Probe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 Probe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: strtok question

that new approach worked! but i also want to say:
if it is 50 words... then stop or if there is a double line break then stop

what i mean by double line break is:
line 1 with stuff written in it *press enter*
empty line - line break *press enter*

Reply With Quote
  #8  
Old August 10th, 2002, 12:21 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: strtok question

don't mean to confuse but.. try, yet something else!
php Code:
Original - php Code
  1.  
  2. <?
  3. $words[] = explode("n", $quotes);
  4. for($i=0; $i<49; $i++)
  5. {
  6.     print("$words[$i]<br>n");
  7. }
  8. ?>

this is more like your original code so maybe it'll make more sense. regular expressions take longer to execute anyway psshh

Reply With Quote
  #9  
Old August 10th, 2002, 02:51 AM
Probe Probe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 Probe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: strtok question

all i got with that were the words array

Reply With Quote
  #10  
Old August 10th, 2002, 07:57 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: strtok question

maybe i didn't understand what you wanted.. sorry

Reply With Quote
  #11  
Old August 10th, 2002, 02:09 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: strtok question

Notepad: How can you say that regular expression are longer to execute ?
When I code I sometimes ask myself for the quickest code but since I can't find any statistics on the time taken by functions, I tend to write code the simpliest way ?

If you have pointers to such stats, I'll be happpy to see them B-)

Reply With Quote
  #12  
Old August 11th, 2002, 01:14 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: strtok question

i'm just going on what i've read in books, remember i'm self-taught here so please correct me if the following statement can be proven otherwise.. and i quote from: PHP and MySQL Web Development - by Luke Welling & Laura Thomson. Chapter 4 page 116

"In general, the regular expression functions run less efficiently than the string functions with similar functionality. If your application is simple enough to use string expressions, do so."

Reply With Quote
  #13  
Old August 11th, 2002, 01:31 AM
Gipz Gipz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Stockholm, Sweden
Posts: 98 Gipz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to Gipz
RE: strtok question

I modified notpad's code to stop if "dubbel linebreak".

php Code:
Original - php Code
  1.  
  2. <?
  3. $words[] = explode("n", $quotes, 51); // dont need to explode more than 50 words
  4. for($i=0; $i<49; $i++)
  5. {
  6.     if(empty($words[$i]))
  7.         break;
  8.     else
  9.         print("$words[$i]<br>n");
  10. }
  11. ?>


haven't tested it but it should do it.

Reply With Quote
  #14  
Old August 12th, 2002, 03:07 PM
Probe Probe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 7 Probe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: strtok question

all i get is the words array again???

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > strtok question


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