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,275
  • 3
  • 25
  • 44
Ariel
  • 79
  • 1
  • 9
  • have u tried removing the dash in ur `-
    `?
    – lemonsss Apr 21 '18 at 04:55
  • @lemonsss i need the dash for sentence breaking, i cant find a way to not loop that at the end – Ariel Apr 21 '18 at 04:57
  • So why do you need to loop? You need to post more code for us to see what is going on. – Andreas Apr 21 '18 at 05:00
  • @Andreas oops, wrong term sorry, the chunk split function still prints out the dash at the end of the string even if it doesnt exceed the limit – Ariel Apr 21 '18 at 05:02
  • @Ariel I see this as an [XY Problem](https://meta.stackexchange.com/q/66377/352329). This is a developer-applied separation of content, but the decision should be left to client-side. Let the table cell decide how to present overflowing text. I wouldn't add hyphens because it might confuse readers about whether there is actually a hyphen in the original text. Imagine if someone copy-pastes your content, they are going to collect more characters than actually exist. What if the user changes their browser font size / zoom-in/out? See also https://stackoverflow.com/a/73175505/2943403 – mickmackusa Jul 30 '22 at 21:11

5 Answers5

2

Try to use preg_replace() to replace string at end with chunk_split

<td valign="top">'.preg_replace('/-<br>$/', '',chunk_split($row['interventions'],20,"-<br>")).'</td>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
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,610
  • 6
  • 30
  • 62
0

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>
jvk
  • 2,133
  • 3
  • 19
  • 28
  • 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
  • For obvious reasons, this answer is provably incorrect. The last character is not a hyphen, so rtrimming with a hyphen is useless. https://3v4l.org/ZNCEF – mickmackusa Jul 30 '22 at 12:41
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