Page 2 of 3
Posted: Tue Apr 03, 2007 2:54 pm
by RobertGonzalez
You don't pass the code that creates the function through the data loop. You pass the loop data through the function.
Post the code you are using that is generating the errors so we can show how to fix it.
EDIT | Looking back on your code, you might be wanting to do something like this...
Code: Select all
<?php
while ($row = mysql_fetch_array($result)) {
echo truncate_long_string($row['title']);
}
?>
Posted: Tue Apr 03, 2007 3:03 pm
by Benjamin
This should do the trick..
Code: Select all
<?php
$string = "This is a string with words in it. I like strings with words in it. They are not as fun as silly string but I still enjoy them.";
function truncate($string, $len)
{
$x = explode(" ", $string);
$truncated_string = null;
while (strlen($truncated_string) < $len) $truncated_string .= ' ' . array_shift($x);
return $truncated_string . '...';
}
echo truncate($string, 15);
Posted: Tue Apr 03, 2007 3:11 pm
by RobertGonzalez
I was thinking an example might be in order for you. Try this one...
Code: Select all
<?php
/**
* Cuts a string down to a certain length without cutting words.
*
* This function, snagged from the PHP net website, cuts a long string
* into a smaller string without cutting words in half. After the string
* is cut the function appends ' ...' to the end of the string.
*
* @access public
* @param string $string The string to cut short
* @param integer $max_length The length to cut the string to
* @return string A shortened string
*/
function truncate_long_string($string, $max_length) {
if (strlen($string) > $max_length) {
$string = substr($string, 0, $max_length);
if (($pos = strrpos($string, ' ')) === false) {
return substr($string, 0, $max_length) . ' ...';
}
return substr($string, 0, $pos) . ' ...';
} else {
return $string;
}
}
/*
$test_string = 'If only there were a function that would cut strings down but not cut words in the process.';
echo truncate_long_string($test_string, 15) . '<br />';
echo truncate_long_string($test_string, 25) . '<br />';
echo truncate_long_string($test_string, 40) . '<br />';
echo truncate_long_string($test_string, 55) . '<br />';
echo truncate_long_string($test_string, 150) . '<br />';
*/
/**
* Lets test the function out with an array...
* this sort of simulates a long string coming out of a database
*/
$string_array = array();
$string_array[] = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras iaculis vulputate pede. Curabitur tincidunt';
$string_array[] = 'adipiscing nisl. Etiam dignissim vestibulum libero. Sed sit amet nunc vitae purus blandit molestie. Etiam';
$string_array[] = 'at tortor. Curabitur et lorem. Donec quis neque. Donec at dolor. Nulla a justo. Lorem ipsum dolor sit';
$string_array[] = 'amet, consectetuer adipiscing elit.';
$string_array[] = 'Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin nulla odio,';
$string_array[] = 'ornare sit amet, tristique et, fermentum vitae, odio. Nam dictum urna in eros. Fusce malesuada, sem vel';
$string_array[] = 'posuere vestibulum, libero nisi lacinia erat, eget ornare mauris nisl ut arcu. Ut nibh massa, dapibus sit';
$string_array[] = 'amet, venenatis vel, vehicula ac, nunc. Nullam sollicitudin semper magna. Mauris id tellus in ante sodales' ;
$string_array[] = 'fermentum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.';
$string_array[] = 'Nulla faucibus volutpat augue. Ut vulputate nulla sit amet odio. Maecenas rutrum. Proin lacinia ipsum eu metus.';
$string_array[] = 'Donec feugiat laoreet risus. Sed augue elit, posuere et, elementum sit amet, viverra eget, mauris. Proin quam.';
$string_array[] = 'Phasellus risus erat, dictum eget, euismod ut, posuere ac, erat.';
$string_array[] = 'Aliquam non odio. Phasellus ligula dui, gravida vitae, varius non, posuere at, ligula. In convallis, mauris';
$string_array[] = 'et lacinia elementum, felis libero ornare elit, accumsan consequat arcu turpis eu odio. Nulla facilisi.';
$string_array[] = 'Mauris venenatis, felis et porttitor gravida, nunc risus semper eros, vitae mattis nulla turpis nec enim.';
$string_array[] = 'Donec vel tortor. Duis consectetuer rhoncus elit. Duis nec eros. Mauris sagittis. Fusce elit. Integer';
$string_array[] = 'scelerisque tristique turpis. Proin volutpat sapien at erat. Pellentesque tincidunt, nisl at accumsan';
$string_array[] = 'dictum, nulla dolor porta pede, a fermentum ante pede ut purus. Integer lacinia, purus sit amet sagittis';
$string_array[] = 'aliquet, odio leo varius nunc, vel rhoncus pede tellus ut arcu.';
$string_array[] = 'In porttitor, turpis et varius gravida, metus ligula bibendum purus, ac tincidunt nunc tortor a diam.';
$string_array[] = 'Vivamus semper. Quisque fringilla, ipsum non pretium commodo, mauris neque nonummy massa, ac lacinia';
$string_array[] = 'tortor pede nonummy lorem. Vivamus scelerisque mollis orci. In ac lorem id tortor laoreet vestibulum.';
$string_array[] = 'Aenean congue dui non arcu. Aliquam erat volutpat. Nulla aliquam nulla a magna. Aenean ornare pretium';
$string_array[] = 'dolor. Integer ut purus. In suscipit tempor est.';
$string_array[] = 'Aliquam erat volutpat. Duis faucibus nulla quis neque. Quisque ac quam. Cum sociis natoque penatibus';
$string_array[] = 'et magnis dis parturient montes, nascetur ridiculus mus. Aliquam cursus ligula et tellus pharetra lobortis.';
$string_array[] = 'Sed velit dui, sagittis ut, dignissim vitae, rutrum in, odio. Proin sed odio. Sed auctor, eros euismod';
$string_array[] = 'cursus placerat, purus purus convallis leo, eget mollis odio odio condimentum urna. Aenean euismod laoreet';
$string_array[] = 'felis. Mauris vitae augue id augue euismod tincidunt. Fusce congue, libero at luctus ornare, turpis diam';
$string_array[] = 'lobortis elit, ac laoreet mauris orci sit amet turpis. Nulla a tellus vel erat semper mollis. Morbi ut';
$string_array[] = 'arcu vitae eros accumsan malesuada. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur';
$string_array[] = 'ridiculus mus. Integer ullamcorper felis blandit est. Vivamus ornare.';
/**
* To what length do we wish to cut the string?
*/
$string_cut = 30;
/**
* For clean coding, set our loop limit here
*/
$string_count = count($string_array);
/**
* Start outputting
*/
echo '<ul>';
for ($i = 0; $i < $string_count; $i++)
{
echo '<li>' . truncate_long_string($string_array[$i], $string_cut) . '</li>';
}
echo '</ul>';
Posted: Tue Apr 03, 2007 3:13 pm
by Benjamin
Use Everah's, mine was just a quick 5 minute job and it doesn't check whether there are spaces in the string or if the passed string is actually less than $len.
Posted: Tue Apr 03, 2007 3:28 pm
by Sinemacula
Everah wrote:You don't pass the code that creates the function through the data loop. You pass the loop data through the function.
Oops!
Everah wrote:I was thinking an example might be in order for you. Try this one...
Okay, now I get it... and it works great!!
I also see very clearly what I was doing wrong... and have a much better understanding of how pre-defined functions work vs. putting the functional code right in place.
Thanks, Everah (and everyone else who responded)!!!
Scott
Posted: Tue Apr 03, 2007 3:30 pm
by RobertGonzalez
Glad we could help. Even cooler that you are local (to me anyway).
Posted: Tue Apr 03, 2007 11:01 pm
by Kieran Huggins
My turn:
Code: Select all
function tease($string,$length){
$a = explode(md5($string),wordwrap($string,$length,md5($string)));
return $a[0];
}
echo tease("If only there were a function that would cut strings down but not cut words in the process.",50);
Posted: Wed Apr 04, 2007 4:45 am
by stereofrog
Posted: Wed Apr 04, 2007 8:42 am
by Kieran Huggins
onion++
Posted: Wed Apr 04, 2007 10:34 am
by RobertGonzalez
I like onion's example also, but it only works if you are querying. What if the user wanted an all around function to handle strings and arrays that are processed from, say, a language file or flat text file?
Posted: Wed Apr 04, 2007 3:10 pm
by Sinemacula
I now have a new, somewhat related task to modify and chop a string:
I've got long book titles that I need to "chop" the string after (and including) the ":" and then remove all non-word characters from what's left, and replace all spaces with "+".
Here's what I came up with:
Code: Select all
$spaces = array(" ");
$commas = array(",", ":", "&", "(", ")");
$nocolon = explode(":",$books[2]);
$pluses = str_replace($spaces, "+", $nocolon[0]);
$searchbooks = str_replace($commas, "", $pluses);
So, since you were all so much help the first time 'round, I thought I'd just check to see if this is a good way of doing it... it does what I need, but could it be cleaner/faster/more efficient?
Thanks,
Scott
Posted: Wed Apr 04, 2007 3:25 pm
by RobertGonzalez
Why are you replacing spaces with +? And
strpos() might be of use to you if you are looking for a particular string.
Posted: Wed Apr 04, 2007 4:04 pm
by Sinemacula
Everah wrote:Why are you replacing spaces with +?
I need to go from something like this:
"Beginning PHP and MySQL 5: From Novice to Professional, Second Edition"
to something like this:
"Beginning+PHP+and+MySQL+5"
to be inserted into the url for a search in an Amazon.com aStore (I need to have the links search for specific titles, but I can't use ISBN numbers because there may be multiple editions that would be acceptable).
Everah wrote:And
strpos() might be of use to you if you are looking for a particular string.
Well, I don't really care where the ":" is, I just want it, and everything after it, removed from the string. I've found that searches that include all the words from subtitles often come up empty, so I figured it was best to just eliminate subtitle words from the search url that is being produced.
Posted: Wed Apr 04, 2007 4:11 pm
by RobertGonzalez
urlencode()
Sinemacula wrote:Well, I don't really care where the ":" is, I just want it, and everything after it, removed from the string.
You really can't do anything about it without knowing where it is or going after it in some capacity

. You can use regexp or you can use strpos() if you know there will only be one colon in the string.
Posted: Wed Apr 04, 2007 4:19 pm
by Sinemacula