too long

I want to generate specific characters string. I need to generate exact 35 characters string for that I am using this function.

var_dump(str_pad("some text some text", 35, " ", STR_PAD_RIGHT));

So, it's giving me this output:

string(35) "some text some text "

That's exactly fine. It'll add blank spaces for remaining characters.

But, For this type of scenario, I want to discard characters above 35.

var_dump(str_pad("some text some text some text some text some text some text some text some text", 35, " ", STR_PAD_RIGHT));

Current Output:

string(79) "some text some text some text some text some text some text some text some text"

I need it 35 characters here as well by discarding last few characters from string.

if (mb_strlen($string,"UTF-8") > 34){
    //there are 35 or more characters to the string
    $string = mb_substr($string,0,35,"UTF-8");
}
else {
    //there are 34 or less characters so carry on
    $string = str_pad($string, 35, " ", STR_PAD_RIGHT));
}

Using mb_substr to remove the string length exceeding the 35 characters limit. This is the MultiByte variation which I think should be recommended for counting.

Try using substr on the initial string to cut it down to 35 characters and then padding:

$data = "some text some text some text some text some text some text some text some text";
$data35 = str_pad(substr($data, 0, 35), 35, ' ', STR_PAD_RIGHT);

var_dump($data35);
/*
    will output:
    string(35) "some text some text some text some "
*/