3

I am looping a row which has long characters (long sentence). As you can see I put a "-
" at the end of the chunk split for proper sentence breaks. I am just wondering why does it show a "-" at the last end of the string?

 <td valign="top">'.chunk_split($row['interventions'],20,"-<br>").'</td>

enter image description here

Mehravish Temkar
  • 4,030
  • 3
  • 24
  • 40
Ariel
  • 79
  • 1
  • 8

5 Answers5

2

Try to add rtrim() at end with chunk_split

<td valign="top">'.rtrim(chunk_split($row['interventions'],20,"-<br>"),"-<br>").'</td>
B. Desai
  • 16,264
  • 5
  • 24
  • 44
1

Here is how to remove special character from string.

How to remove last comma from string using php?

<td valign="top">'.rtrim(chunk_split($row['interventions'],20,"-<br>"), "-").'</td>
Krishna Jonnalagadda
  • 1,954
  • 1
  • 12
  • 26
  • many thanks bro :), i just saw the solution the same time you posted the answer lol https://stackoverflow.com/questions/25433351/how-to-make-so-chunk-split-doesnt-add-something-at-the-end-of-the-string anyways thank you :) – Ariel Apr 21 '18 at 05:06
1

You can use str_split to make an array of the string, then join it back with implode.
Implode will not end the string with the delimiter.

$arr = str_split("abcdefghijklmn",5);
Echo implode("-<br>", $arr);

https://3v4l.org/DMj9E

$arr = str_split($row['interventions'],20);

Echo '<td valign="top">'. implode("-<br>", $arr) .'</td>';
Andreas
  • 23,304
  • 5
  • 28
  • 61
0

This solved by

<td valign="top">'.substr(chunk_split($row['interventions'],20,"-<br>"), 0, -5).'</td>

Here -5 if length if

"-<br>"

Get help from http://php.net/manual/en/function.chunk-split.php#39321

hkjamil
  • 100
  • 1
  • 8
-1

There is a - character in:

<td valign="top">'.chunk_split($row['interventions'],20,"-<br>").'</td>

Fix it by using:

<td valign="top">'.chunk_split($row['interventions'],20,"<br>").'</td>
alkaliexce
  • 25
  • 3
  • 1
    removing the dash will remove all dashes from the loop – Ariel Apr 21 '18 at 05:00
  • Ah i see. Okay, if your concern is about the word-wrapping hyphens, then i recommend you solve that via CSS or JS instead. Doing that in php will just make it difficult for yourself later on since ure lumping the formatting and logic together. Check out https://stackoverflow.com/questions/856307/wordwrap-a-very-long-string/856322#856322 – alkaliexce Apr 21 '18 at 05:02