EExcerpt using Low “Find and Replace” - Anyone good with RegEx?
Posted: 23 December 2009 04:57 PM   [ Ignore ]  
Summer Student
Total Posts:  16
Joined  05-02-2009

Hi all,

I’m currently using EExcerpt to create short summary views of each entry on my blog.  While it does a great job stripping out all the images from each post, oftentimes I will have captions alongside those images that will remain intact.

I am attempting to format the results of my EExcerpt query using Low’s “Find and Replace” plugin along with a custom RegEx to identify the caption code and strip it out as well.

Each one of my captions is formatted via a custom style in Wygwam that wraps them in the following code:

<class="caption"></p

...so I have been attempting to use the following RegEx to identify that snippet:

<p[^>]*class\s*=\s*(['\"])caption\1[^>]*>(.*?)</p> 

My final code block looks like this:

{exp:md_eexcerpt if_exceeds="50" stop_after="50"}
    {exp
:replace find="<p[^>]*class\s*=\s*(['\"])caption\1[^>]*>(.*?)</p>" regex="yes"}
    {body}
    {/exp:replace}
{/exp:md_eexcerpt} 

This code gives me the following error:

Warningpreg_replace() [function.preg-replace]No ending delimiter '/' found in /public_html/system/plugins/pi.replace.php on line 72 

I guess my first question would be: do any of you guys have experience with this type of implementation?  Am I going about this the wrong way?  Part of me is thinking this wouldn’t work regardless, as EExcerpt might strip HTML from the result anyway, so there’d be nothing to match the expression to.

And if that’s not the case, is there any chance someone might be able to point me in the right direction with my RegEx code?

Thanks!

Profile
 
 
Posted: 23 December 2009 06:22 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  129
Joined  04-10-2006

My guess is that it comes from the closing paragraph :

</p

Looks like the slash has been used as a delimiter in the preg_replace function so you’ll have to escape it :

<\/p

Hope this will work for you !

 Signature 

Benjamin David | Duktee - Add-ons for ExpressionEngine : Video Player, Mini CP, Entry Layout, Site Switch, Google Rank Tracker, Google Translate, Downloader & more…

Profile
 
 
Posted: 23 December 2009 06:27 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  129
Joined  04-10-2006

You might also have a look at Kses, a PHP script that does a great job at cleaning HTML tags and attributes. Someone should do an Expression Engine Add-On with this one smile

http://sourceforge.net/projects/kses/

 Signature 

Benjamin David | Duktee - Add-ons for ExpressionEngine : Video Player, Mini CP, Entry Layout, Site Switch, Google Rank Tracker, Google Translate, Downloader & more…

Profile
 
 
Posted: 23 December 2009 11:20 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  16
Joined  05-02-2009

Benjamin,

Thanks!  That definitely took care of the error, but still didn’t strip the HTML.  I’ve got a feeling that it’s interfering with the EExcerpt plugin, which might be stripping the tags before the RegEx gets a chance to process them.  Maybe this isn’t the way to go after all.

Thanks for the heads up on Kses, I will definitely check it out!

- Jon

Profile
 
 
Posted: 24 December 2009 10:11 AM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1847
Joined  12-18-2008

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

regex doesn’t parse HTML.  you can almost always come up with some sort of html that will break a regex.

That said, if you know your data very well you might be able to use something like this as your regex:

<class="caption">.*?</p

the key here is the .*? which is a lazy or non-greedy operation.  This should keep the regex from consuming everything until the last </p> in your field (which would almost certainly be more than you want to delete).

 Signature 

EE Pro Network
eMarketSouth - full service web design, development, and SEO
ExpressionEngine consulting services - SQL queries, .htaccess rewrites, template optimization, jQuery/AJAX, and more!
our add-ons - please “favorite” and rate our products on devot:ee if you like our work. smile

Profile
 
 
Posted: 11 February 2010 02:57 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  265
Joined  06-26-2008

Hi Juan Largo,

I’m having this exact same problem. Any luck finding a solution?

Thanks!

Profile