Ready to get started?Download WordPress


Almost there! Get string before "more" tag (teaser) from PAGES - HELP w/ RegEx (4 posts)

  1. Zambrano Sergio
    Posted 4 years ago #

    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)


    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 65

    This 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">
    $testim_page = randomTestimonial();
    echo $testim_page->post_excerpt; ?>&quot; <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>

    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!!


  2. Zambrano Sergio
    Posted 4 years ago #

    The error belongs to this one, also not working.


  3. Zambrano Sergio
    Posted 4 years ago #

    Sorry, I meant the originally mentioned regex threw the error for the first hyphen after the "more" word. The error for that was at offset 21, after I deleted a question mark to test where the error was.

    This regex is the one that throws error in place 22.

    Still not resolved. :(

  4. Zambrano Sergio
    Posted 4 years ago #

    This line did the trick.
    Still don't know why the error.

    $testimonial_teaser = preg_replace('/<!--more.*/s', '', $testim_page->post_content);

Topic Closed

This topic has been closed to new replies.

About this Topic