Almost there! Get string before “more” tag (teaser) from PAGES – HELP w/ RegEx
-
Could you help me to figure out what the problem with this is?
I created a regex to get ONLY the string BEFORE the “more” tag, from a PAGE (to build a teaser)
([^]*)(<!–more(.*?)–>)([^]*)
I’ve managed to get a random page out of the “testimonials” featured on the home page, and I get an error on the regex that gets “everything until the the more tag”.
The error is:
Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 22 in /home/content/xxx/xxxxx/html/xxxxxx/wp-content/themes/Sumo_V2/functions.php on line 65This is the line 65, where the error is:
$testim_page->post_excerpt = preg_replace('/([^]*)(<!--more(.*?)-->)([^]*)/', '$1', $testim_page->post_content);
The whole function that retrieves the random page and creates a “post_excerpt” out of the content is:
function randomTestimonial(){ $pages = get_pages('child_of=177&parent=177'); $randomKey = (mt_rand(1,count($pages)) - 1); $testim_page = $pages[$randomKey]; $testim_page->post_excerpt = preg_replace('/([^]*)(<!--more(.*?)-->)([^]*)/', '$1', $testim_page->post_content); return $testim_page; }
The code on the template is working fine:
<div id="testimonial"> <p>"<?php $testim_page = randomTestimonial(); echo $testim_page->post_excerpt; ?>" <a href="<?php echo get_page_link($testim_page->ID); ?>">read the full story</a></p> <cite><a href="<?php echo get_page_link($testim_page->ID); ?>"><?php echo $testim_page->post_title; ?></a></cite> </div>
If I change one character at that position (in the regex, I assume that’s what the error points to) either the question mark or one hyphen, the regex should still work but it fails and the whole content is retrieved instead.
Help me, please!!
Thanks.
- The topic ‘Almost there! Get string before “more” tag (teaser) from PAGES – HELP w/ RegEx’ is closed to new replies.