Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.