Reputation: 2207
Im trying to clean up values, as some strings are longer than 70 characters, so I need to cut these of.
the idea is that if the length is longer than 70 cut this of after the last comma(remove this to), but some strings that are longer dont have a comma so these need to be cut of at the last whitespace within the max length of 70 characters so that we dont have partial words.
The code that I have now(not working correctly).
$str = substr($longstr, 0 , 70 , '');
$shortstr = substr($str, 0, strrpos($str.",", ","));
output 1 (with commas)
$longstr = 'Some strings are way to long and need to be cut of, yes realy because we need the space.';
$shortstr = 'Some strings are way to long and need to be cut of';
output 2 (whitepsace)
$longstr = 'Some strings are way to long and need to be cut of yes realy because we need the space.';
$shortstr = 'Some strings are way to long and need to be cut of';
Upvotes: 0
Views: 613
Reputation: 481
----EDIT ----
$maxlen = 70;
for ($i = strlen($str); !in_array($str[$i], array(" ", ",")) || $i >= $maxlen ; $i--)
{
$str = substr($str,0,$i);
}
See it working: https://tehplayground.com/3F4zhA4tjCQTiBfc
---- OLD ANSWER ----
not pretty but it works:
<?php
$chrs = array(","," ");
$maxLen = 25;
$str = "This is a , long ass string, with commas, ";
if (strlen($str) > $maxLen)
{
$cut = strrpos(substr($str,0,$maxLen),",");
$cut2 = strrpos(substr($str,0,$maxLen)," ");
if ($cut < $cut2)
{
$cut = $cut2;
}
echo substr($str,0,$cut);
}
strrpos finds the last instance of a character in a string, run that against the comma, and then against the space, which ever is a higher number gets chosen as the cut point
https://tehplayground.com/tKD4vdk6pfupVQan
if you wanna see it working :/
Upvotes: 1
Reputation:
Using your example and regexr to try some stuff out, you can easily do something akin to the following;
<?php
$longstr = 'Some strings are way to long and need to be cut of yes really because we need the space.';
$shortstr = trim(preg_replace('/(.){70}/', "$0", $longstr));
print $shortstr; // Will give; "Some strings are way to long and need to be cut of yes really because
Trim will remove trailing whitespace, and the preg_replace will only get the first 70 chars (inc whitespace until removed) into the shortstr variable
I understand this is not the most appropriate option for larger data sets due to overhead, but this is something I have used in the past to achieve the same sort of result, But to be aware of the following will be beneficial;
To use this, can potentially add slow downs to any page loads times due to the nature of rexex and the overheads that it has
Upvotes: 0
Reputation: 2743
At first, cut the limit the source string (you are right, only the fourth argument is redundant: the substr
function has only three parameters):
$tempstr = substr($longstr, 0, 70);
Next, find the position of a space or comma - depends what of them is closer to end of limited string - and get the string until this position:
$shortstr = substr($tempstr, 0, max(strrpos($tempstr, ' '), strrpos($tempstr, ',')));
Upvotes: 1
Reputation: 299
Suggestion turn string into array then loop through to check for comma if it has comma cut off at comma else cut off at first whitespace after 70
$arr1 = str_split($str);
if(in_array(',', $arr1){
if(count($arr1 > 70) {
//cut off at comma
}
} else {
if(count($arr1 > 70) {
//cut off at whitespace
}
}
Upvotes: 0