The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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!!

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...

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($article, strpos($article, '<p>') + 3, 1);

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