We help IT Professionals succeed at work.

Cutting up long strings in forum post

St_Aug_Beach_Bum
on
Medium Priority
254 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
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

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
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

Web Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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!
Or re-use an old wheel:

http://www.php.net/wordwrap

Author

Commented:
Did try wordwrap, but there were some oddities there, didn't quite work right, but thank you.
Didn't see your ps...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.