php word wrap function needed - one that works

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • leegold2

    php word wrap function needed - one that works

    I have been looking for a php word wrap function. I know there's an
    official PHP function but I've tried that and many of the functions
    contributed on that php.net page and none do what I want...and some act
    weird as well.

    I want, If I say break at 150 chars then the function will break at the
    nearest space to 150 ie. it will actually break at the word boundry. And
    if theres a url it will not break inside a URL, it will leave URLS alone.

    Please state/link me *specifically* what function if you know of one,
    Thanks for the help,
    Lee G.
  • jblanch

    #2
    Re: php word wrap function needed - one that works

    function iTrunc($string, $length) {
    if (strlen($string )<=$length) {
    return $string;
    }

    $pos = strrpos($string ,".");
    if ($pos>=$length-4) {
    $string = substr($string, 0,$length-4);
    $pos = strrpos($string ,".");
    }
    if ($pos>=$length* 0.4) {
    return substr($string, 0,$pos+1)." ...";
    }

    $pos = strrpos($string ," ");
    if ($pos>=$length-4) {
    $string = substr($string, 0,$length-4);
    $pos = strrpos($string ," ");
    }
    if ($pos>=$length* 0.4) {
    return substr($string, 0,$pos)." ...";
    }

    return substr($string, 0,$length-4)." ...";

    }
    from an unknown author..
    but with slight modification you should be able to get what you want..
    this just takes the string, finds the nearest period, word end, or
    space at the spec. length and returns the string. This could be
    helpful, i'm not sure if it solves any problems.

    Comment

    • leegold2

      #3
      Re: php word wrap function needed - one that works

      jblanch wrote:
      [color=blue]
      > function iTrunc($string, $length) {
      > if (strlen($string )<=$length) {
      > return $string;
      > }
      >
      > $pos = strrpos($string ,".");
      > if ($pos>=$length-4) {
      > $string = substr($string, 0,$length-4);
      > $pos = strrpos($string ,".");
      > }
      > if ($pos>=$length* 0.4) {
      > return substr($string, 0,$pos+1)." ...";
      > }
      >
      > $pos = strrpos($string ," ");
      > if ($pos>=$length-4) {
      > $string = substr($string, 0,$length-4);
      > $pos = strrpos($string ," ");
      > }
      > if ($pos>=$length* 0.4) {
      > return substr($string, 0,$pos)." ...";
      > }
      >
      > return substr($string, 0,$length-4)." ...";
      >
      > }
      > from an unknown author..
      > but with slight modification you should be able to get what you want..
      > this just takes the string, finds the nearest period, word end, or
      > space at the spec. length and returns the string. This could be
      > helpful, i'm not sure if it solves any problems.[/color]

      I don't know if I can modfiy this to ignore links...[color=blue]
      >[/color]

      Comment

      • Janwillem Borleffs

        #4
        Re: php word wrap function needed - one that works

        leegold2 wrote:[color=blue]
        > I don't know if I can modfiy this to ignore links...[/color]

        One approach would be to let wordwrap do its stuff and replace the unwanted
        breaks, example:

        function mywordwrap ($str, $length) {
        $str = wordwrap($str, $length);
        return nl2br(
        preg_replace(
        "/<([^>]*)[\r\n]([^>]*)>/s",
        "<$1 $2>",
        $str
        )
        );
        }


        JW



        Comment

        • Manuel Lemos

          #5
          Re: php word wrap function needed - one that works

          Hello,

          on 01/02/2005 03:58 AM leegold2 said the following:[color=blue]
          > I have been looking for a php word wrap function. I know there's an
          > official PHP function but I've tried that and many of the functions
          > contributed on that php.net page and none do what I want...and some act
          > weird as well.
          >
          > I want, If I say break at 150 chars then the function will break at the
          > nearest space to 150 ie. it will actually break at the word boundry. And
          > if theres a url it will not break inside a URL, it will leave URLS alone.
          >
          > Please state/link me *specifically* what function if you know of one,[/color]

          Yes, I also had to ditch PHP wordwrap function because of its bugs. I
          use my own implementation in a class that I use composing and sending
          e-mail message. It is used to break long lines and optionally mark them
          with some quoting marks if necessary.

          It is just meant to break plain text but you are free to modify it to
          skip HTML tags:




          --

          Regards,
          Manuel Lemos

          PHP Classes - Free ready to use OOP components written in PHP
          Free PHP Classes and Objects 2025 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


          PHP Reviews - Reviews of PHP books and other products


          Metastorage - Data object relational mapping layer generator

          Comment

          • Janwillem Borleffs

            #6
            Re: php word wrap function needed - one that works

            leegold2 wrote:[color=blue]
            > I don't know if I can modfiy this to ignore links...[/color]

            One approach would be to let wordwrap do its stuff and replace the unwanted
            breaks, example:

            function mywordwrap ($str, $length) {
            $str = wordwrap($str, $length);
            return nl2br(
            preg_replace(
            "/<([^>]*)[\r\n]([^>]*)>/s",
            "<$1 $2>",
            $str
            )
            );
            }


            JW



            Comment

            • Justin Koivisto

              #7
              Re: php word wrap function needed - one that works

              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              leegold2 wrote:

              | I have been looking for a php word wrap function. I know there's an
              | official PHP function but I've tried that and many of the functions
              | contributed on that php.net page and none do what I want...and some act
              | weird as well.
              |
              | I want, If I say break at 150 chars then the function will break at the
              | nearest space to 150 ie. it will actually break at the word boundry. And
              | if theres a url it will not break inside a URL, it will leave URLS alone.
              |
              | Please state/link me *specifically* what function if you know of one,
              | Thanks for the help,
              | Lee G.

              Here is one that I had thrown together a while back:

              function word_wrap($char s,$str){
              ~ $cpy=strip_tags ($str);
              ~ $chk=array_reve rse(preg_split( '`\s`',$cpy));
              ~ $chk2=array_rev erse(preg_split ('`\s`',$str));
              ~ $len=0;
              ~ $retVal='';

              ~ // we want to work backwards on this
              ~ for($i=count($c hk)-1;$i>=0;$i--){
              ~ // $len is the current segment length in the stripped string
              ~ if($len>0 && ($len + strlen($chk[$i])) > $chars){
              ~ // add a line break
              ~ $retVal.='<br />'."\n";
              ~ $len=0;
              ~ }else if($len>0){
              ~ // space between words needs to be counted
              ~ $len++;
              ~ }

              ~ // add the necessary pieces to the string
              ~ $pop1=array_pop ($chk); // get next piece from each version
              ~ $pop2=array_pop ($chk2);
              ~ $retVal.=$pop2. ' ';
              ~ $len+=strlen($p op1);
              ~ $pattern='`'.pr eg_quote($pop1) .'`';
              ~ while(!preg_mat ch($pattern,str ip_tags($pop2)) ){
              ~ // if pop1 and pop2 are not referencing the same element
              ~ $pop2=array_pop ($chk2);
              ~ $retVal.=$pop2. ' ';
              ~ if($pop2==NULL) break;
              ~ }
              ~ }
              ~ return $retVal;
              }

              It wraps plain text or HTML as it displays (doesn't count the HTML tags
              when processing). The string is split on whitespace, if there is a piece
              that is longer than the wrap, it outputs just that as it was input.

              HTH

              - --
              Justin Koivisto - justin@koivi.co m

              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.2.1 (MingW32)
              Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

              iD8DBQFB2tvhm2S xQ7JEbpoRAoeZAJ 9XilXn/AgPBSpdAQe6ihCv pAQKqQCdFVkt
              BkTpkYMcJYqJxA8 R552govQ=
              =IcB0
              -----END PGP SIGNATURE-----

              Comment

              Working...