• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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.
0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now