SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Jan 7, 2002, 06:16 #1
- Join Date
- Jan 2002
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
limiting field view to X words - problem with loop
Hi there,
I have function which takes text from mysql and displays limited number of words with [more]. It also search for nearest space to avoid breaking the word in a middle.
It was compiled from 2 other func. so I have a little problem with infinite loop in while statement..
function is:
function Cut($article_desc,$url) {
return CutConvert($article_desc).'...' . '[<a href="'.$url.'">more</a>]';
}
function CutConvert($str, $index=30, $char=" ") {
while (substr($str, $index, 1)!=$char) $index++;
return substr($str, 0, $index);
}
How to fix potential infinite loop in 'while'?
In the code above if the last character in the $str is
not blank, it will loop forever because the substr() will return an empty string "" and "" <> " ", so it loops forever...
pls help
-
Jan 7, 2002, 08:19 #2
You can actually do this in your query:
SELECT SUBSTRING_INDEX(field, ' ', 30) as shortext from table;
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Jan 7, 2002, 08:42 #3
- Join Date
- Jan 2002
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
unfortunately, I cannot
..becouse query is generated by CodeCharge software, and code I need have to be included manually as 'global' funcion, then recalled in specific page.
So I can put parameter (number of chars) in 2 places only:
- at function definition
- at recall function (with specific db field) on php page.
-
Jan 7, 2002, 08:51 #4
Why can't you just edit the code created by CodeCharge, or possibly code it yourself?
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Jan 7, 2002, 09:02 #5
- Join Date
- Jan 2002
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
becouse I'm still working and regenerating whole site 20 times a day..
and I know site will be under continuous developing (using Codecharge) in the future..
Bookmarks