Jump to content

wordwrap help


zohab

Recommended Posts

Is there any way to break single long string?

 

One way to do it:

 

<?php

$string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
echo breakString($string, "<br />\n", 15);

function breakString($string, $delimeter = "<br />\n", $breakAt=15) {
$breaks = ceil(strlen($string) / $breakAt); // break every 15 characters
$newString = "";
for ($i=0; $i<$breaks; $i++) {
	$newString .= substr($string, ($i * $breakAt), $breakAt) . $delimeter;
}

return $newString;
}
?>

 

EDIT:

Made it into a function.

Link to comment
https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973482
Share on other sites

Why not just use the last parameter of wordwrap? It'll cut long words if you pass true to its value

echo wordwrap("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccchghggggggggggggggggf",10, "<br />\n", true);

 

lol because I did not think to look at that :) and I thought it would be fun to write a function, but yea. That is definitely the better solution!

Link to comment
https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973490
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.