Hm- how’s that for a vague description? Anyway, just a simple little plugin to do a show/hide in entries. Basically, I had some js and was just putting it in the template to show/hide the body text- but that wasn’t very flexible. I wanted to put it in the actual article- deciding IF and WHERE I wanted show/hide to kick in. Well, this turned out to be mostly easy- I just added a new html button that outputs:
[lj-cut] and [/lj-cut], and if I want to ‘cut’, click the button. Then I wrote a really simple plugin to go in and replace [lj-cut] with my javascript. So basically, the plugin looks like this:
$dummy = array(”[/lj-cut]”, “[lj-cut]”);
$replace = array(”<a href=\“javascript:void(0);\” onclick=\“showHide($entry_id,0,this,‘entry’);return true;\”>Read Less…</a></div>”, “<div id=\“extLink$entry_id\”>
<a href=\“javascript:void(0);\” name=\“ext$entry_id\” onclick=\“showHide($entry_id,’#’,this,‘entry’);return false;\”>Read More…</a></div><div id=\“extText$entry_id\” style=\“display: none\”>”);
return str_replace($dummy, $replace, $TMPL->tagdata);
See it in action here- the second and third articles respectively.
BUT, two problems:
1. I really want to put in a failsafe in case the closing [/lj-cut] is missing- because if it’s missing, the div doesn’t close and the rest of the articles end up under the ‘hide’. It’s a given- I’ll forget to close it and I’ll forget to notice.
2. You can only use the [lj-cut] once per article. This isn’t a huge problem, but I’d like to fix it. Not critical, though- and I can imagine having more than one ‘hide’ could make fixing #1 more difficult. #1 is kinda important. Anyway- it’s a javascript thing- you can see it in the header in the link- it’s calling the div by id, so you can only have one. Unfortunately, I don’t know javascript- seems to me if you switched it to use class, it would work. Of course, each ‘read’ link would open the whole article, but that would be fine. Right now, the second ‘read’ link just stays hidden no matter what- which is less fine. But like I say- having two cuts in the same article is non-critical. THAT I can remember not to do!
OK- so on to problem 1- I figure two approaches-
a. IF [lj-cut] is found, just insert the closing javascript bit automatically- don’t even bother with a search/replace. Honestly, I’d be cool with that- but I’ve no bloody clue how to test IF there was a string replacement and I’ve no clue how to just instert that bit of javascript right before the closing tag of the plugin. Seriously- no clue, not even where to start digging through the manual.
b. Do some kind of count- and if the number of [lj-cut]‘s is larger than the number of [/lj-cut]‘s, instert the appropriate number of closing tags just before the closing tag of the plugin. Obviously- this brilliant plan also suffers from me having no clue how to echo out the closing javascript- plus no clue how to count the replacements. Actually- looks like php5 has a count function, but that does me no good.
This function over in the php manual discussion gave me some counting ideas:
function str_replace_count($find,$replace,$subject,$count)
{
$subjectnew = $subject;
$pos = strpos($subject,$find);
if ($pos !== FALSE)
{
while ($pos !== FALSE)
{
$nC = $nC + 1;
$temp = substr($subjectnew,$pos+strlen($find));
$subjectnew = substr($subjectnew,0,$pos) . $replace . $temp;
if ($nC >= $count)
{
break;
}
$pos = strpos($subjectnew,$find);
} // closes the while loop
} // closes the if
return $subjectnew;
}
$stuff = “a b a b a b a b a”;
print $stuff . ”—the old string<br>\n”;
print str_replace_count(“a “,“c “,$stuff,4) . ”—the new string<br>\n”;
// will output c b c b c b c b a—the new string
Maybe I could limit the replacement of the [lj-cut] to one, and if $count = 1 echo out the closing javascript? Hm- that seems in the ballpark. Um- so how the heck do I do the string replace and THEN do the echoey part.
Ugh- think I’ll go have a beer. Insights appreciated.
