php - Using preg_match Check IF Keyword Exists in Wordpress Post Content

113

I am trying to figure out how I can effectively search the post content for a keyword that can be comprised of one or multiple words ( i.e. word, two words, and three words, etc. ) and retrieved using a variable ( i.e. $meta_keyword_1, $meta_keyword_2 ).

I have a function that updates custom fields based upon the results of a keyword search. I have been successful in searching the post title for a keyword using stripos. I was NOT able to successfully use stripos to search the post content. I was able to research and come up with an example of a preg_match, but it keeps returning '0' or FALSE even when the exact match is present in the content.

Here are the processes within my function:

1). Retrieve keyword and replace "-" for " " for searching purposes:

NOTE: Meta keyword is inputted as: word, two-words, and-three-words, blue-rabbit, red-rabbit

$meta_keyword_1_clean = str_replace("-", " ", $meta_keyword_1);

$meta_keyword_2_clean = str_replace("-", " ", $meta_keyword_2);

2). Search IF keyword exists in post content:

$post_content_strip_1 = preg_match('/('.$meta_keyword_1_clean.')/', $post->post_content);

$post_content_strip_2 = preg_match('/('.$meta_keyword_2_clean.')/', $post->post_content);

3). Execute IF statement if keyword exists, else continue to next IF statement if does not exist:

<?php

elseif ( $post_content_strip_1 !== false  ) {

    // For testing purposes, Update post meta #1 with $post_content_strip_1 value
    // Update post meta #2 with $meta_keyword_1 value

} elseif ( $post_content_strip_2 !== false ) {

    // For testing purposes, Update post meta #1 with $post_content_strip_2 value
    // Update post meta #2 with $meta_keyword_2 value

} else {

    return false;

}

Here are my results:

Match keywords found on post content: word, two words

Match keywords NOT found on post content: blue rabbit, red rabbit

a). Using '!== false' + $meta_keyword_1 = 'word' ( MATCH ) --> First IF statement executed, output '0' and 'word';

b). Using '!== false' + $meta_keyword_1 = 'two-words' ( MATCH ) --> First IF statement executed, output '0' and 'two-words';

c). Using '!== false' + $meta_keyword_1 = 'blue-rabbit' ( NON-MATCH ) --> First IF statement executed, output '0' and 'blue-rabbit';

d). Using '!== 0' + $meta_keyword_1 = 'word' ( MATCH ) --> Second IF statement executed, $meta_keyword_2 = two-words ( MATCH ), output '0' and 'two-words';

e). Using '!== 0' + $meta_keyword_1 = 'blue-rabbit' ( NON-MATCH ) --> Second IF statement executed, $meta_keyword_2 = red-rabbit ( NON-MATCH ), output '0' and 'red-rabbit';

f). Using '=== false' + $meta_keyword_1 = 'word' ( MATCH ) --> Second IF statement executed, $meta_keyword_2 = red-rabbit ( NON-MATCH ), output '0' and 'red-rabbit';

g). Using '=== false' + $meta_keyword_1 = 'blue-rabbit' ( NON-MATCH ) --> Second IF statement executed, $meta_keyword_2 = red-rabbit ( NON-MATCH ), output '0' and 'red-rabbit';

My problems:

1). I feel like the preg_match pattern is not properly setup to produce an accurante result of found or not-found. My previous attempts at using preg_match would not produce a value ( i.e. 0 or 1 ), with various examples I have been able to progress it this far;

2). My keywords are inputted as lowercase but post content could be varying. The searches should be case insensitive, I believe that is achieved with a '/i' at the end of the pattern?;

If you think I am missing something, please let me know. Any help would be greatly appreciated.

Thanks in advance!

UPDATE #1:

To better illustrate my problem, I believe the reason why my keyword searches of the post content are failing is because the preg_match pattern is incorrectly setup. The complication, including my lack of understanding, are the variable ( $meta_keyword_1_clean or $meta_keyword_2_clean ) that are inputted, and the modifiers that come before and after.

This produces a FALSE result always:

preg_match('/('.$meta_keyword_2_clean.')/', $post->post_content);

This and variations of it produces errors:

preg_match('/'.$meta_keyword_2_clean.'/', $post->post_content);

I am looking for help in producing this:

preg_match([something-here].$meta_keyword_2_clean.[something-here], $post->post_content);

I hope this clarifies my need.

UPDATE #2:

I decided to research a little bit more on the subject after Jan's comment ( Thank You Jan! ) and I was able to find someone that asked the same question, perhaps with much better precision. Answer to my problem here: Strpos with exact matches. I hope this helps someone else finding this question.

People are also looking for solutions to the problem: php - Display a specific slide on page load based on the link selected

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.