I need to add a closing tag after string replace..
Posted: 19 March 2005 10:09 PM   [ Ignore ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23536
Joined  05-20-2002

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.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 19 March 2005 11:10 PM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9868
Joined  06-19-2002

Well, I kind of got lost about half-way through there, but I know that you can use the “Filter HTML” Plugin to automatically close opened tags.  So, that might be useful for you here.

 Signature 

Chris Curtis
chriscurtis.org

Profile
 
 
Posted: 20 March 2005 12:07 AM   [ Ignore ]   [ # 2 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23536
Joined  05-20-2002

I thought of that one- but it’s not really a tag, and I’m not sure which would kick in first- my plugin or the Filter html.  Either way, I think I’d have to go into the backend and add the tag- either the [lj-cut] or the javascript.

This kinda works- basically do a substring count, and if there are more lj-cuts than /lj-cuts, tack another closing javascript on the end (which ended up being easy).  It would be better to calculate the difference, and add the correct number of closing tags- and to be thorough, make it work if there were more closing tags than opening tags- though that level of thorough seems silly.  Also, I’m not sure how hard a hit it is to do all of this substring counting- anyone know?  If it’s a killer, I’ll take it out and just try to be careful with my closing tags- or get filter html working on it.

Eh, anyway, I’ll play with the counting thing tomorow, but I figure what I’ve got is good enough for me.  (Oh- and I’m not ignoring my email, I’m just slow getting to it!  Cause I’m screwing around with this instead.  And now I’m going to bed! ;o)

Working sample below- any improvements appreciated.

  global $TMPL;
$entry_id = $TMPL->fetch_param(‘entry_id’);
$closed = ‘’;
$numopen = substr_count($TMPL->tagdata, “[lj-cut]”);
$numclosed = substr_count($TMPL->tagdata, “[/lj-cut]”);
if ($numopen > $numclosed) {
$closed = “<a href=\“javascript:void(0);\” onclick=\“showHide($entry_id,0,this,‘entry’);return true;\”>Read Less…</a></div>”;
}

$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).$closed;

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 20 March 2005 11:02 AM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9868
Joined  06-19-2002

> I’m not sure which would kick in first- my plugin or the Filter html

That should just depend on the parse order/direction you set for the Plugins.  You should be able to ensure that your Plugin is parsed before Filter HTML.

 Signature 

Chris Curtis
chriscurtis.org

Profile
 
 
   
 
 
‹‹ EE Tag In A Plugin      entry linking plugin ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64978 Total Logged-in Users: 25
Total Topics: 82017 Total Anonymous Users: 18
Total Replies: 440817 Total Guests: 173
Total Posts: 522834    
Members ( View Memberlist )