SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict tedleonard's Avatar
    Join Date
    Jun 2000
    Location
    Dark side of the moon
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question A kind of, search and replace

    Hiya,
    You guys at sitepoint have been a great help to me, so I thought you might be the right ones to ask about my latest dilemma. I'm trying to write a search & replace script... kinda. I need php to take a block of text (in this case an article), take only the very first letter of the very first paragraph, and replace it with HTML for an image of that letter. You get what I'm looking for here? Sometimes I'm not all that good at explaining what I'm after. But basically I just need it to auto-detect the first letter of an article and replace it with a big fancy letter. Thanks in advance!!

    Ted

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how will you know where the first character of the article is? Is it alone in a text-based file? XML? Database? Free-form HTML?
    ----Adopt-a-Sig----
    Your message here!

  3. #3
    SitePoint Addict tedleonard's Avatar
    Join Date
    Jun 2000
    Location
    Dark side of the moon
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm pulling it out of a mysql database and putting it onto a php-driven page.

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One way you could do it (and it depends upon who is submitting the articles) is to have the authors put a special character, or series of characters just before the first letter of each paragraph. OR you could test for the "\n" character and then replace the character immediately following the "\n" (new line) character with the graphic. OR...

    There are many ways you could do this.
    John

  5. #5
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this what you wanted. I assumed that you have an image for every letter both capital and small letters:

    PHP Code:
    $article 'Once upon a time, there was a ... ;) ';

    // the image based on the first character of the article
    $image '<img src="path/to/images/'.(substr($article01)).'" width="50" height="50">';

    // now need to remove the first character of the article
    $article substr($article1strlen($article));

    // put everything together and output it
    echo $image $article

  6. #6
    SitePoint Addict tedleonard's Avatar
    Join Date
    Jun 2000
    Location
    Dark side of the moon
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for all your help!! Chris that is just what I was looking for, I'm gonna try out your code and let you know if it works, thanks so much!!

  7. #7
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought about my previous version and it will take the first character of a string not the first of the first paragraph.
    If you said paragraph, I thought that you were using "<p>" and then this should work:

    PHP Code:
    $article "This is a heading and is not to be included
    <p>Once upon a time, there was a ... ;) </p>
    <p>later they found out that ...</p>"
    ;

    $letter substr($articlestrpos($article'<p>') + 31);

    // the image based on the first character of the article
    $image '<img src="path/to/images/'.$letter.'.gif" width="50" height="50">';

    // now need to remove the first character of the article
    $heading substr($article0strpos($article'<p>'));
    $article $heading '<p>'$image substr($articlestrpos($article'<p>') + 4strlen($article));

    // put everything together and output it
    echo '<pre>';
    echo 
    htmlspecialchars($article);
    echo 
    '</pre>'

  8. #8
    SitePoint Addict tedleonard's Avatar
    Join Date
    Jun 2000
    Location
    Dark side of the moon
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alas!! It worked!! Thanks so much for your help Chris!!!


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
  •