Support » Fixing WordPress » PHP Problem – counter doesn’t increment

  • Ok, I don’t think I’m a complete gimp, but I’m proly a partial one.
    I know this isn’t a WP-specific issue, but I’m putting this here because I know we have some talented PHP’ers out there.
    The background: I want to add a dropcase letter to my website, and I only want it on the first letter of each part, not on every paragraph, so using CSS is out of the question. I’m trying to do this as a plugin. So, what I’m trying to do is as each section is processed, I grab the content (passed in as $content), grab the first letter and change the markup. The problem is that sometimes the first thing is some HTML. So I want to skip over the HTML, find the first displayable letter, pull it out of the HTML, and then prepend it so that the dropcase is then followed by the rest of the content.
    This should give you an idea of what I’m talking about.
    So, here’s my code that I am using:
    $newContent = trim($content);
    $ltrPos = 0;
    $openTag = '';
    $first_letter = substr($newContent,0,1); // Get the first letter
    if ($first_letter == '<') {
    //The text is marked up and we have an opening tag
    //Need to get past that to find the first chr. Should
    // be able to find it by searching for the matching >
    while ($firstLetter == '<') {
    while(substr($newContent,$ltrPos,1) != '>')
    $ltrPos +=1; // $ltrPos + 1;
    $openTag = substr($newContent, 0, $ltrPos+1);
    $first_letter = substr($newContent,$ltrPos+1,0);
    $newContent = "<div class='drop-case' id='".$ltrPos."'>".$first_letter."</div>".$openTag.substr($newContent,$cnt+2);
    }else {
    $newContent = "<div class='drop-case'>".$first_letter."</div>".substr($newContent,1);
    return $newContent;

    From what I can tell, my $ltrPos isn’t being incremented. I can tell this because I pring out the variable in the ID attribute, and it’s 0 everytime. I can tell that it’s going into the top part, where $first_letter == ‘<‘ but it doesn’t seem to be going through either of the while loops.
    Can any one see what I may have smegged up?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Well CSS may not be out of the question. Surely there is a CSS selector for the beginning of storycontent?

    Yes techgnome here it is the pseudo class .storycontent: first-letter
    Won’t that do it?

    yeah, that only works in FF/Mozi but doesn’t work for IE…..All I need to do is extract the first character of the post, wrap it in it’s own DIV tag with a class of drop-case, and then CSS can take it from there.
    Using the pure CSS method required using content p:first-letter {…} which was OK, but does all paragraphs…. some one clued me into using content p+p:first-letter {…} to over ride for remaining paragraphs, but it broke in IE. Also it would let me set the image behind the letter and let the text float around it.
    So I’ve opted the brute force method to get it cross-browser. I hope that if I can get it all to work, I can release a plugin complete with an admin screen to allow someone to set up how they want the drop case to look.

    .storycontent:first-letter {…} ?
    Hmmm….. let me try it and see what happens…. but I don’t think the image will show…

    Well it should do. The fact you got it running for all paras shows it is right. The first-letter can be applied to any block element. Apparently. Is this a typo thing?
    Or a browser issue? Just some ideas. I am interested because when you get it running it is going to be very cool. What about a variation of first-line and first letter combined?

    OK, Root…. goto the link I gave in my first post. This is how it now looks with the .storycontent p:first-letter {…} and tell me how horrible that looks. And despite the fact I’ve set a height & width on it, the full background image doesn’t show, only enough based on the size of the image. Secondly, it’s now on every paragraph. I don’t want that. If I want, I *could* set up a .storycontent p+p:first-letter {…} to overwrite the remaining paragraphs, but that DOESN’T work in IE, and end up getting the same result as I do right this instant.
    Are you on IRC? This would be sooo much easier that way.

    Never been keen on IRC. This looks like two css qs combined.
    Q1. Is what is the relevant selector. Just about solved. Q2. Is how can you style an inline element (one letter) with a bg etc? A span is needed presumably?

    Well you need a php guru. But I am going to keep trying on the CSS. I think Stop Design had an article on this.

    *sigh* Isn’t that what I said? ;P Unfortunatly to make it cross-browser I don’t think pure CSS will work, but if you can find away, that’ld be cool.

    OK, we can call off the search, I found out what was wrong with my code. I started using $first_letter, but then switched to $firstLetter! Ugh! I feel like a moron now.

    Ha-Ha! I win!!! I got the smeggin thing working! Check it out:

    TG – Gave it a whirl, looks nice but will have to look into tweaking it a bit as I don’t cotton to going through old posts and editung those that start with a span, image or blockquote (fatal error – execution time). If I get to it, I will let you know what I came up with to fix the problem.

    Beel – eh? It should jsut skip right over…… Oh…. I think I get you know….. hmmm….. I wonder what I can do about that…..
    I’ll re-visit this in a day or two and see if there’s something I can work out…..

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘PHP Problem – counter doesn’t increment’ is closed to new replies.