Web Analytics Made Easy -
StatCounter How to PHP wordwrap() by pixels, not by chars? - CodingForum

Announcement

Collapse
No announcement yet.

How to PHP wordwrap() by pixels, not by chars?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to PHP wordwrap() by pixels, not by chars?

    Hi,

    I know how to use the wordwrap(), but I would need help to create one that wraps the words by pixels, not by characters. Is this even possible? That would help me a lot if it's possible.

    Thank you for your help.
    PHP 5 & MySQL 5 (Y)

  • #2
    i'm sorry, but i don't understand the question. could you give an example or so?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      He wants it so PHP wraps the line after a certian amount of pixles.
      Not possible, or so I think.

      Comment


      • #4
        I dont think there is any PHP function that does this but,
        you could try taking the max width to wrap in pixels, dividing it by the font width then using that number as the number of characters.
        Ofcourse you would need fixed font sizes

        PHP Code:
        $fontwidth 8;
        $wrapsize 160;

        $wrappedtext wordwrap($textfloor($wrapsize/$fontwidth)); 
        Its not a perfect solution but I guess it depends what you're trying to do.
        http://www.hazelryan.co.uk

        Comment


        • #5
          wouldn't it be easier to put the text in a div and then set the width for that div?
          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

          Comment


          • #6
            Well I have a table that has maximum width value but that's not enough because if I enter the value to be for example 128, this would not work:

            http://www.testtexttesttexttesttextt...ttesttext.com/

            That would make the table to be over streched

            Help? Or does div max width do the same than table max width?
            PHP 5 & MySQL 5 (Y)

            Comment


            • #7
              wordwrap with the cut-parameter set to 1 should be all you need. I don't understand what pixels have to do with it...
              PHP Code:
              $wrappedtext wordwrap($text25"\n"1); 

              Edit: i think i might finally understand the pixel thing -->you want the text to wrap the visible part of your link and not the actual tag, because wrapping the html-tag would brake the link.

              if you don't want the wrapping to brake links, then check out the comments on http://www.php.net/manual/en/function.wordwrap.php. sollution posted there:
              PHP Code:
              $body 'this is a text with aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa looooooooooooooooooooooooooooong word! <a href="http://ww.this.site.com/with/a/loooooooooooooooooooooong/url"> ... </a>';

              foreach(
              explode(" "strip_tags($body)) as $key => $line) {
                if (
              strlen($line) > 35$body str_replace($linewordwrap($line25" "1), $body);

              Last edited by raf; Oct 5, 2006, 07:36 AM.
              Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

              Comment


              • #8
                Thanks for your help raf! I'll put that code in my safe place to make sure I don't forget it.

                In my site, there are sidebars that are about 160 pixels width I think. If user enter text which width is bigger, it will stretch the sidebar and thats irritating. Webbrowser tries to wrap the words but if a one word is longer then 160 pixels then it will not wrap it. LikeThisMightBeLongerThan160PixelsOrWhatDoYouThink?????
                PHP 5 & MySQL 5 (Y)

                Comment


                • #9
                  not sure if this is solved or what you still asking for, because we don't see your html. if your page-section is still stretched, then you should test how many letters you can have in that 160 pixel width and adjust the above code (adjust the 35 and 25).
                  Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                  Comment


                  • #10
                    If instead of a <table> tag you use a <div> tag with a css style of overflow: hidden or even overflow: scroll then it will not break your page format to have extra long words.

                    Comment

                    Working...
                    X