SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert a div half way through content

    Lets say I want to insert a div roughly half way through a content item text. Is there a function that could determine the end of a sentence at this point in the string?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you explain the case in a bit detail?

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a news article as a string: $new_content
    I need to put an adsense div floated in the middle of it.
    So I guess best thing to do is to split the string into 2 paragraphs (but at a sensible place... ie at end of a sentence), then insert my div and then add the rest of the string:

    <p>1st half of string</p>
    <div>my ad floated</div>
    <p>2nd half of string</p>

  4. #4
    SitePoint Zealot adrian_schmidt's Avatar
    Join Date
    Jul 2010
    Location
    Malmö, Sweden
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless you can find the code already written, I think you'll need to write your own function to look for punctuation indicating the end of a sentence. I don't think there's any built in function for that.

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually just realised that the text is made up of a series of paragraphs seperated by \n
    So i'm thinking i could explode on those and then count the array...then rebuild with the div inserted?

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would do that skyline if i can't handle it with regex, or with strstr() http://www.php.net/strstr

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finding the end of a sentence would be difficult (period? period followed by space and capital? e.g. This is not the end ... ). If you have a string and new lines, just find the middle (strlen), go forward to the next new line (strpos), and hope for the best

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2010
    Location
    Netherlands
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The paragraph option would be the best. An adsense block in the middle of a sentence is not really user-friendly. Although I'm not sure if \n would be the best point to split the text. I figure if you split on any </p> occurrences you'd achieve your goal in the best possible way (assuming you use HTML with paragraph tags in your text).


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
  •