0

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';
user759235
  • 2,147
  • 3
  • 39
  • 77
  • 1
    may shed some light https://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-of-chara – Kevin Jun 20 '18 at 09:34

4 Answers4

1

----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 :/

Craig B
  • 499
  • 4
  • 13
  • not a worry, see my revised option, much neater, and scalable, if you wish to add extra cut points (i.e. "-" "_", just add them to the array – Craig B Jun 20 '18 at 09:59
1

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, ',')));
Timurib
  • 2,735
  • 16
  • 29
  • ah thats much tidier than my option, we had the same idea, but yours is simpler. i forgot about the max function. – Craig B Jun 20 '18 at 09:44
0

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

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • this will output 'because we need the space.' so the end of the string, instead of the begin – user759235 Jun 20 '18 at 09:45
  • @user759235 - have you tried this on regexr? Testing this does print the beginning for me? – Can O' Spam Jun 20 '18 at 09:46
  • @SamSwift웃 use a php testbad (i.e. tehplayground.com and it only returns the latter section (everything post 70 chars)). – Craig B Jun 20 '18 at 09:47
  • My bad - $0 is what you need, will update, 0 based indexes are a pain at times! – Can O' Spam Jun 20 '18 at 09:49
  • still didnt seem to work, it now just returns the full line, only replaces the first 70 with themself :/ – Craig B Jun 20 '18 at 10:01
  • @CraigB - well, yes, it's supposed to return the first 70 chars of a string, the point is this is an example of what can be done to collect that, the OP could make it much better by having checks and get exactly what's needed – Can O' Spam Jun 20 '18 at 10:13
0

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
  }
}
Avi Teller
  • 299
  • 1
  • 2
  • 8