SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a text file that will be written to with data from end users (text datatype).

    The text document is formatted such that there is a column of text around 200px wide, set in the middle of the page.

    Is there a way I can (or function that will) format a string (perhaps a paragraph or two) so that it will fit in this text document? Mind that the string is not a constant - it is entered by end users so I can't exactly hard code \n\t.

    Thanks in advance,
    - Mike
    http://www.georgiaoffroad.com

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, its just about my bed time - so maybe that explains it - but I've got no idea what you are trying to do

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here's the text that is in the database:

    Code:
    Snipped as it was causing the page width to be too wide to read...   
    Just imagine a real long string without breaks being here :)

    And, providing (code)(/code) preserves formatting.... Here is how the text should appear in the text document:

    Code:
                         We arrived at the track around noon. 
                         The parking lot was cram-packed, though 
                         we lucked up and found a spot in the 
                         shade. After unloading and getting geared 
                         up, we fired up the bikes. Mine's been cold-
                         starting very well lately. I began using a drill 
                         I read about in an old issue of Dirt Bike -- 
                         put the bike in second and rock back and 
                         forth a few times; choke, kick, lights right 
                         up on first or second kick.
    Last edited by mstembri; Apr 18, 2001 at 08:38.
    - Mike
    http://www.georgiaoffroad.com

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inserting characters in a string

    Ok, I've an idea of how I want to structure this...

    Code:
    	$columnwidth = 45; // width of text column
    	$tabs = "\n\t\t\t\t";  // line break and spacer	
    	$length = strlen($notes);  // length of notes
    	$lengthcount = 0;  // counter
    
    	while ($lengthcount <= $length) {
                              // function to insert $tabs goes here		
    		$lengthcount = $lengthcount + $columnwidth;
    	}
    Trick is I am not sure of the function to insert text in a string. This unknown function would be set up like..

    insert_function ($notes, $stringtoinsert, [ INT number of characters over to place $stringtoinsert]

    I'm looking on php.net, so far no dice.
    - Mike
    http://www.georgiaoffroad.com

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oic - so it is a text file? Otherwise if you where outputing to a html file you would
    use <div> to format the column. but I think you would have thought about that.
    Anyway, ... This should reformat the contents of $string to do what you want. (untested code).

    1) To format the text into a fixed width column use wordwrap()
    http://www.php.net/manual/en/function.wordwrap.php

    2) Now you have to manipulate the string so that you insert some whitespace
    at the begining of each line. So I would do simething like:
    PHP Code:
    $string wordwrap($string$width);
    $stringArray explode('\n'$string);
    $whitespace '          ';  // string to fill whitespace at begining of each line
    foreach($stringArray as $line) {
       
    $newStringArray[] = $whitespace $line;
    }
    $string implode('\n'$newStringArray); 

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This works

    Code:
    	$columnwidth = 48; // width of text column
    	$tabs = "\n\t\t\t\t\t   ";  // line break and spacer	
    	$length = strlen($notes);  // lenght of notes
    	$lengthcount = 0;  // counter
    
    	while ($lengthcount <= $length) {
    		$lengthcount = $lengthcount + $columnwidth;
    		$notes = substr_replace($notes, $tabs, $lengthcount, 0);
    	}
    It chops words in half at the breaks, but it works well enough for my purposes.
    - Mike
    http://www.georgiaoffroad.com

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid

    1) To format the text into a fixed width column use wordwrap()
    http://www.php.net/manual/en/function.wordwrap.php
    Aha, just what I was looking for.

    I'll modify my code above. Thanks!
    - Mike
    http://www.georgiaoffroad.com

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for kicks and giggles, I tried your script...

    It works fine in terms of wrapping words, but the whitespace feature only works on the first line... Ideas?
    - Mike
    http://www.georgiaoffroad.com

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, '\n' is not evaluated properly in the explode and implode functions. I should have used "\n" which is evaluated to mean the new line character. I modified my code and tested. Here is my test code which now works:
    PHP Code:
    <?php
    $string 
    "this is my string of text I want to wrap blah blah blah";
    $width 12;
    $string wordwrap($string$width);
    $arr explode("\n"$string);
    $whitespace "          ";
    foreach (
    $arr as $line) {
       
    $paddedString .= $whitespace $line "\n";
    }
    echo 
    $paddedString "\n";
    ?>

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is the best way of doing this for an online email sender, i want it to wrap so the emails comes out well.

    will using
    <textarea name="message" cols="55" rows="15" value="" wrap="hard" style="background-color:CCCCFF"></textarea>

    and /n at the end of each line? and what browsers will this work on? sorry for all the questions!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •