Forum Consecutive Character Problem !

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
stibos
Forum Newbie
Posts: 2
Joined: Wed Jul 12, 2006 11:47 am

Forum Consecutive Character Problem !

Post 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 :(
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
stibos
Forum Newbie
Posts: 2
Joined: Wed Jul 12, 2006 11:47 am

Post 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

:)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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()
Post Reply