Skip to main content
Cater for embedded closed angle pairs; use proper $wpdb stuff.
Source Link
bonger
  • 4.4k
  • 1
  • 19
  • 25

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    global $wpdb;
    if ( ! preg_match( '/wp_posts' . $wpdb->posts . '\.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $q_search$search_str = esc_sql( preg_quote( stripslashes( $matches[1][0] );
 ) );  // Cater for closed angle pairs embedded in the search string.
    $regexsfor ( $i = array0, $len = mb_strlen( $search_str ); $i < $len; $i++ ) {
    $regexs[]    $q_searches[] = '^''(<[^>]*>)?' . $q_search;preg_quote( mb_substr( $search_str, $i, 1 ) );
    $regexs[]}
    $q_search = '^[^<]+'implode( .'', $q_search;$q_searches );
    $regexs[] = '<[^>]*>''^[^<]*' . $q_search; // Before any angle bracket.
    $regexs[] = '<[^>]*>[^<]+''(<[^>]*>)[^<]*' . $q_search;
  // After any closed $regexangle =bracket 'wp_postspair.post_content 
 RLIKE \'' . array_unshift( $regexs, implode( '\'' OR wp_posts', array_fill( 0, count( $regexs ), $wpdb->posts . '.post_content RLIKE \''%s' ) ) );
    $search_replace = call_user_func_array( array( $wpdb, $regexs'prepare' ), .$regexs '\'';);
    $search = substr( $search, 0, $matches[0][1] ) . $regex$search_replace . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}
add_filter( 'posts_search', 'wpse159789_posts_search', 10, 2 );

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    if ( ! preg_match( '/wp_posts.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $q_search = esc_sql( preg_quote( stripslashes( $matches[1][0] ) ) );
    $regexs = array();
    $regexs[] = '^' . $q_search;
    $regexs[] = '^[^<]+' . $q_search;
    $regexs[] = '<[^>]*>' . $q_search;
    $regexs[] = '<[^>]*>[^<]+' . $q_search;
     $regex = 'wp_posts.post_content RLIKE \'' . implode( '\' OR wp_posts.post_content RLIKE \'', $regexs ) . '\'';
    $search = substr( $search, 0, $matches[0][1] ) . $regex . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}
add_filter( 'posts_search', 'wpse159789_posts_search', 10, 2 );

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    global $wpdb;
    if ( ! preg_match( '/' . $wpdb->posts . '\.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $search_str = stripslashes( $matches[1][0] );
    // Cater for closed angle pairs embedded in the search string.
    for ( $i = 0, $len = mb_strlen( $search_str ); $i < $len; $i++ ) {
        $q_searches[] = '(<[^>]*>)?' . preg_quote( mb_substr( $search_str, $i, 1 ) );
    }
    $q_search = implode( '', $q_searches );
    $regexs[] = '^[^<]*' . $q_search; // Before any angle bracket.
    $regexs[] = '(<[^>]*>)[^<]*' . $q_search; // After any closed angle bracket pair. 
    array_unshift( $regexs, implode( ' OR ', array_fill( 0, count( $regexs ), $wpdb->posts . '.post_content RLIKE %s' ) ) );
    $search_replace = call_user_func_array( array( $wpdb, 'prepare' ), $regexs );
    $search = substr( $search, 0, $matches[0][1] ) . $search_replace . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).

Fix escaping of search string.
Source Link
bonger
  • 4.4k
  • 1
  • 19
  • 25

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    if ( ! preg_match( '/wp_posts.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $q_search = esc_sql( preg_quote( stripslashes( $matches[1][0] ) ) );
    $regexs = array();
    $regexs[] = '^' . $q_search;
    $regexs[] = '^[^<]+' . $q_search;
    $regexs[] = '<[^>]*>' . $q_search;
    $regexs[] = '<[^>]*>[^<]+' . $q_search;
    $regex = 'wp_posts.post_content RLIKE \'' . implode( '\' OR wp_posts.post_content RLIKE \'', $regexs ) . '\'';
    $search = substr( $search, 0, $matches[0][1] ) . $regex . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}
add_filter( 'posts_search', 'wpse159789_posts_search', 10, 2 );

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    if ( ! preg_match( '/wp_posts.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $q_search = preg_quote( $matches[1][0] );
    $regexs = array();
    $regexs[] = '^' . $q_search;
    $regexs[] = '^[^<]+' . $q_search;
    $regexs[] = '<[^>]*>' . $q_search;
    $regexs[] = '<[^>]*>[^<]+' . $q_search;
    $regex = 'wp_posts.post_content RLIKE \'' . implode( '\' OR wp_posts.post_content RLIKE \'', $regexs ) . '\'';
    $search = substr( $search, 0, $matches[0][1] ) . $regex . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}
add_filter( 'posts_search', 'wpse159789_posts_search', 10, 2 );

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    if ( ! preg_match( '/wp_posts.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $q_search = esc_sql( preg_quote( stripslashes( $matches[1][0] ) ) );
    $regexs = array();
    $regexs[] = '^' . $q_search;
    $regexs[] = '^[^<]+' . $q_search;
    $regexs[] = '<[^>]*>' . $q_search;
    $regexs[] = '<[^>]*>[^<]+' . $q_search;
    $regex = 'wp_posts.post_content RLIKE \'' . implode( '\' OR wp_posts.post_content RLIKE \'', $regexs ) . '\'';
    $search = substr( $search, 0, $matches[0][1] ) . $regex . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}
add_filter( 'posts_search', 'wpse159789_posts_search', 10, 2 );

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).

Source Link
bonger
  • 4.4k
  • 1
  • 19
  • 25

You could try using several regexps to only search outside angle brackets:

function wpse159789_posts_search( $search, $query ) {
    if ( ! preg_match( '/wp_posts.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
        return $search;
    }
    $q_search = preg_quote( $matches[1][0] );
    $regexs = array();
    $regexs[] = '^' . $q_search;
    $regexs[] = '^[^<]+' . $q_search;
    $regexs[] = '<[^>]*>' . $q_search;
    $regexs[] = '<[^>]*>[^<]+' . $q_search;
    $regex = 'wp_posts.post_content RLIKE \'' . implode( '\' OR wp_posts.post_content RLIKE \'', $regexs ) . '\'';
    $search = substr( $search, 0, $matches[0][1] ) . $regex . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
    return $search;
}
add_filter( 'posts_search', 'wpse159789_posts_search', 10, 2 );

I'm not sure this covers all cases, but seems to work on limited testing ... (it'd be a lot simpler if MySQL regexp supported look behind/ahead).