• 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 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">
                <p>&quot;<?php
    $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>
    		</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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Zambrano Sergio

    (@sergiozambrano)

    The error belongs to this one, also not working.

    ([^]*)(<!–more(.*?)?–>)([^]*)

    Thread Starter Zambrano Sergio

    (@sergiozambrano)

    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.
    ([^]*)(<!–more(.*?)?–>)([^]*)

    Still not resolved. ??

    Thread Starter Zambrano Sergio

    (@sergiozambrano)

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

    $testimonial_teaser = preg_replace(‘/<!–more.*/s’, ”, $testim_page->post_content);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Almost there! Get string before “more” tag (teaser) from PAGES – HELP w/ RegEx’ is closed to new replies.