Page 1 of 1

Forum Consecutive Character Problem !

Posted: Wed Jul 12, 2006 11:54 am
by stibos
I'm sure this must have been touched on before but I can't find an example of it - Does anyone know the regular expression to limit the number of conseuctive non breaking characters (i.e a string not including spaces or breaks) to a certain number?

Its giving me a styling nightmare when someone chucks in something with a consecutive string of greater than 60 characters 8O 8O :(

Posted: Wed Jul 12, 2006 12:49 pm
by RobertGonzalez
Can you substr() their posted string to a size that makes sense to your design? Of course, you could always do an explode() on each ' ' in the posted string, then measure each array members string length. Seems like a lot of overhead though.

Posted: Thu Jul 13, 2006 4:15 am
by stibos
Thanks for the reply Everah, yep that would be the easy thing to do but i don't want to limit the title length (or even the message body length).

I was wondering if someone had a regular expression written for this very problem:

opppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp

:)

Posted: Thu Jul 13, 2006 4:20 am
by Benjamin
Give this a try.. haven't tested or used it though..

Code: Select all

// textwrap 1.1 Brian Moon <brian@phorum.org>
  // This code is part of the Phorum project <http://phorum.org>

  // $String     The string to be wrapped.
  // $breaksAt   How many characters each line should be.
  // $breakStr   What character should be used to cause a break.
  // $padStr     Allows for the wrapped lines to be padded at the begining.
  
  function textwrap ($String, $breaksAt = 78, $breakStr = "\n", $padStr="") {

    $newString="";
    $lines=explode($breakStr, $String);
    $cnt=count($lines);
    for($x=0;$x<$cnt;$x++){
      if(strlen($lines[$x])>$breaksAt){
        $str=$lines[$x];
        while(strlen($str)>$breaksAt){
          $pos=strrpos(chop(substr($str, 0, $breaksAt)), " ");
          if ($pos == false) {
            break;
          }
          $newString.=$padStr.substr($str, 0, $pos).$breakStr;
          $str=trim(substr($str, $pos));
        }
        $newString.=$padStr.$str.$breakStr;
      }
      else{
        $newString.=$padStr.$lines[$x].$breakStr;
      }
    }
    return $newString;

  } // end textwrap()