PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
$text = 'Jack went to the store to get himself a large bottle of whiskey';
preg_match('#^\s*(.{26,}?)\s+.*$#', $text, $match);
var_export($match);
?>
Please tell us exactly what you want to do, since I've got the feeling that this code is not what you real want.
I think what you really need is substr().
I want to have the results from a mysql query displayed such that they will be truncated at 15 characters AND such that if truncation would happen in the middle of a word, it will go to the next space before cutting it off.
So,
if $text is less than 15 characters, the whole of $text is displayed
if $text is more than 15 characters, it will be truncated at the 15th character
BUT, if the 15th character of $text is in the middle of a word, it will go to the next space
I don't know, but MySQL might have a built-in function which does exactly this thing for you - if such a function exists, it will most likely be faster than doing it on the PHP side.
So, in this case, it's $book that I want to display only 15 characters of in one place, but I want the whole thing displaying in another place... so I still need to have the whole value of $book available. What I've got there works fine, except for when $book is less than 15 characters long - in which case it displays nothing for $match[1].
Actually, I need the "whole thing" in another "place" -- same page, same statement even (notice $book and $match[1]), so only one query (in this particular case, since it's creating a url link, I want the shortened version to display, but the full version as the link title).
I tried some of the functions examples in the php manual pages, but they wouldn't work because I'm using it in a recursive section, and I kept getting an error message that I could not "redeclare" the function.
However, I found that adding a strlen() statement did what I need, so I've got:
Shortly after posting that I'd found a working solution, I had a hunch to try something else... I think this solution is better and solves the actual problem I was having, rather than being a "workaround" that might not work for all scenarios.
<?php
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.';
/**
* Outputs: If only there ...
*/
echo truncate_long_string($test_string, 15) . '<br />';
/**
* Outputs: If only there were a ...
*/
echo truncate_long_string($test_string, 25) . '<br />';
/**
* Outputs: If only there were a function that ...
*/
echo truncate_long_string($test_string, 40) . '<br />';
/**
* Outputs: If only there were a function that would cut strings ...
*/
echo truncate_long_string($test_string, 55) . '<br />';
/**
* Outputs: If only there were a function that would cut strings down but not cut words in the process.
*/
echo truncate_long_string($test_string, 150) . '<br />';
?>
Everah wrote:This is really not that hard. In fact, I got this from the manual page that I had linked to in my last post, a few comments down...
Unfortunately, the code you posted, just like the other functions I tried from the manual page you referenced, does not work for my situation... using that code, I get the following error:
I don't know enough to know if there's a way around that, or a "fix" for such functions such that they can deal with the recursive nature of what I'm doing, but the preg_match code followed by the strlen code as I posted above is working fine.
Now, if I could just figure out the right way to do my mysql query as a JOIN rather than three separate queries ( viewtopic.php?p=370960 ), I'd be set!