Cutting up long strings in forum post

I have a forum type site.

When a user submits a commit, it will sometimes have a very long string of continueous characters, usually a long url, which will not wrap and cause the page to strech and the layout to go all wrong.

So the goal is to cut up anything with too many characters. I'm thinking something like this will work, but my skills are exhausted after the explode line.  Will this work and can someone write it?

 $string=explode(" ",$string);
for each string over 70 characters, split the string up into seventy character segments with a <br> in between each.
then reasemble the entire string.

Thank you for any assistance!

PS, I did try wordwrap, but had some inconsistant, unexpected results, just didn't seem to do quite right.
St_Aug_Beach_BumAsked:
Who is Participating?
 
Terry WoodsIT GuruCommented:
And one more bug fix! I've tested this though, and it works. Just change the "wrapat" variable in the function to 70 for your data.
$string = "Some words and a coupleofreallylongones blah blahblahblahblah blah blah";
print "String: $string<br>\n";
$string_array=explode(" ",$string);
 
foreach($string_array as $position => $word) {
  $string_array[$position] = my_wrap($word);
}
 
$string = implode(" ",$string_array);
print "Wrapped string: $string<br>\n";
 
function my_wrap($longstring) {
  $wrapat = 10;
  if (strlen($longstring)>$wrapat) {
    return substr($longstring, 0, $wrapat)."<br>".my_wrap(substr($longstring, $wrapat));
  } else {
    return $longstring;
  }
}

Open in new window

0
 
Terry WoodsIT GuruCommented:
Sounds like a reasonable technique to me.

Give this a try:
$string_array=explode(" ",$string);
 
foreach($string_array as $position => $word) {
  $string_array[$position] = my_wrap($word);
}
 
$string = implode(" ",$string_array);
 
function my_wrap($longstring) {
  if (strlen($longstring)>70) {
    return substr($longstring, 0, 70)."<br>".my_wrap(substr, 70);
  } else {
    return $longstring;
}

Open in new window

0
 
Terry WoodsIT GuruCommented:
Oops - found a bug in that first attempt.
$string_array=explode(" ",$string);
 
foreach($string_array as $position => $word) {
  $string_array[$position] = my_wrap($word);
}
 
$string = implode(" ",$string_array);
 
function my_wrap($longstring) {
  if (strlen($longstring)>70) {
    return substr($longstring, 0, 70)."<br>".my_wrap(substr($longstring, 70));
  } else {
    return $longstring;
}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
St_Aug_Beach_BumAuthor Commented:
Thank you, I'm testing...

This shows my lack of understanding of functions... but the function can be defined after it's usage.  Like it looks to me like you use my_wrap before the function has been defined.

But this seems to work!
0
 
mr_egyptianCommented:
Or re-use an old wheel:

http://www.php.net/wordwrap
0
 
St_Aug_Beach_BumAuthor Commented:
Did try wordwrap, but there were some oddities there, didn't quite work right, but thank you.
0
 
mr_egyptianCommented:
Didn't see your ps...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.