Tricks with text areas?

Discussion in 'PHP' started by mnymkr, Sep 19, 2007.

  1. #1
    I guess I don't mean tricks but code for text areas.

    I learn best by example so I wanted to see what cool stuff can be done with say a 4 sentence text area.

    Can I turn it in in to an array?

    Can I insert stuff after the 3 line on the page (not the sentence)?
     
    mnymkr, Sep 19, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Not quite sure if I understand you, but to split the lines from a textarea into an array, simply do:
    
    $lines = explode("\n", $_POST['textarea']);
    
    PHP:
    Now if every sentence was in a new line, it now is a separate item in the $lines array.
     
    nico_swd, Sep 19, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    my overall question was , what neat tricks can you pull of with text areas?



    but specifically for this:


    say you have a flexible fluid template on a site

    sometimes the third sentence of a blog entry will be 4 lines down or if it is expanded on the second line

    if i wanted to PRINT something after the third line on every blog entry whether it was expanded at 1024 or 800
     
    mnymkr, Sep 19, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    And I was supposed to know what you're asking for from the info you gave in your first post? lol

    So let me rephrase this to make sure I understood you right.

    You want to display something after the third line in a paragraph, independent of the screen resolution and font size? So the line numbers could vary on each computer?

    First, PHP can't do that because it doesn't know the user's screen resolution. MAYBE you could do that with Javascript, but I kinda doubt that too, because there would be no new line characters to split the string. And it would not be possible to figure out in which line you're currently are.

    Plus, the user can change the font size on his browser, and Javascript cannot detect that either.

    And what do blog entries have to do with text areas?


    Also, "neat tricks" is kinda... ugh. Please be more specific. Mind reading does not work over the internet.
     
    nico_swd, Sep 19, 2007 IP
  5. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #5
    sorry

    it wasn't mean to be a specific question

    just if you had a text area, could you turn do some some cool things in it with php.

    could you break it into an array and inject some text maybe an add after the first , second or third sentence?

    could you turn every other word red?

    I guess it is better titled "interesting text area manipulations"

    as far as blog entires and text areas..... isn't a the text on a full on blog entry just a text area in the database?

    the reason i sited this is because i have seen this done.

    I have seen adsense ads injected into a blog entry , always after the 3rd line of text on the screen , not the sentence.

    I was just curious.
     
    mnymkr, Sep 19, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Well, yes you can do all of the things above. And A LOT more, but since you say:

    You expect us to give a code example for a lot of things you're probably not going to use. And it would be a waste of time thinking and coding.

    I don't see why anyone would want to turn every other word red, but if you have the desire to do that, then ask this specific question and people will help.

    Perhaps if you tell us what you're working on, or what you're thinking of, we can be more helpful. This is like saying, "Okay, I have a car. Draw me a map to all places I can drive".

    Understand what I'm trying to say?

    In terms of web, I understand this when you say textarea:
    
    <textarea name="foo"></textarea>
    
    HTML:
    And I guess everyone else would have thought the same.

    Maybe it was coincidence, but I highly doubt that this is possible. Got a link to that page?
     
    nico_swd, Sep 19, 2007 IP
  7. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #7
    again sorry

    it is more curiousity and seeing what could be done.

    the reason for the every other word example is that, well I see how it could be done, i think. However I wouldn't know what to do about the period.

    I appreciate the time you took to answer the things you did.

    I was really just looking at a paragraph and wondering, how could I manipulate this thing with php.

    I thought it would be fun question.
     
    mnymkr, Sep 19, 2007 IP
  8. ErectADirectory

    ErectADirectory Guest

    Messages:
    656
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Use explode() to break sentences apart at the period "." (not perfect because of etc. Mrs. and others) then you can inject your adsense inside of a paragraph.

    Probably the best way to do this is to change
    --> </p><p> <-- to
    --> </p><p>[[adsense code here]]</p><p> <--
    if you are using something like fckeditor. You wouldn't have to use explode by doing this just a simple str_replace().

    Every other color being red would be achieved by explode() also.

    I personally insert a link inside some of the content of my directory. For instance, visit this page and search for " Modern business has changed ", you will see that is a hyperlink. This link is associated with the word "business" but captures surrounding words to make up unique phrases each time it is used on different pages.

    Some may not think this is something interesting to do with textareas because it's not considered a widget ... I think it's pretty cool myself.
     
    ErectADirectory, Sep 19, 2007 IP
  9. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #9
    thats pretty cool can you share a simple version of that?


    I am a chemist by trade and i just do things out of curiousity


    the every other word in red example, i can see how to do it by explode but what happens there when you get to a period
     
    mnymkr, Sep 19, 2007 IP
  10. ErectADirectory

    ErectADirectory Guest

    Messages:
    656
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I'm going to pass on showing my code for the keywords thing. Thanks for the interest though.

    Since it's code you are looking for, below is a 2 minute chop job example for inserting an adsense code inside a paragraph.

    
    
    $text = 'Jojs ofjs ofjsof jij oeijfoseifjse. Doisj foeifjeof fj sejifiosej fijsf alfj. Lj fjaw flija ljaw fja ias flsjaf sif sj flijaf l.  Sjflisae flijfljs fsjf sajf lsajf sjf s j sljfsjil.' ;
    
    $ex_text = explode('.',$text) ;
    $ex_text[2] = $ex_text[2] . '.<br /><br />[[Adsense Code Here]]<br /><br />' ;
    echo str_replace('<br />.','<br />',implode('.',$ex_text)) ;
    
    
    PHP:

     
    ErectADirectory, Sep 19, 2007 IP
  11. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #11
    one last question if you don't mind


    if i just had a string of text as a paragraph


    how could i say show the first 200 letters or just show the first 20 words
     
    mnymkr, Sep 19, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    
    
    // First 200 letters
    echo substr($string, 0, 200);
    
    // First 20 words.
    $words = explode(' ', $string);
    array_splice($words, 20);
    
    echo implode(' ', $words);
    
    PHP:
     
    nico_swd, Sep 19, 2007 IP
  13. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #13
    whats the array splice do?
     
    mnymkr, Sep 19, 2007 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    For further reference, the first thing you should do if you don't know what a function does, is going to www.php.net and typing the function name in the search field.

    All functions are explained there, with a little example code.

    If you went there and still don't understand, then ask and we'll help. :)



    Bookmark this page because it just became your new best friend. :D
     
    nico_swd, Sep 19, 2007 IP