filters - Replace Text with hyperlinks

admin2025-01-07  4

I would like to replace few words in the content of the posts. For example.

Original:

Here is my test string. 
Which Need to Replace First Test word in the string.

Excepted String:

Here is my <a href="link"> test </a> string. 
Which Need to Replace First Test word in the string.

I have tried but there are few scenario where i have faced issues.

When that word has predefined link along with a word. Like

Here is my <a href="test case link"> test Case </a> string. 
Which Need to Replace First Test word in the string.

So my function exploded it in tow words like

Here is my <a href="link"> test </a> <a href="test case link"> Case </a> string.
Which Need to Replace First Test word in the string.

== My Solution ====

add_filter('the_content' , 'update_content');

function update_content($content){
        if((is_singular()){
            $termsList = array(
                 'test' => '<a href="test">test</a>'
            );

            foreach($termsList as $word => $term_link){

                //$content = str_replace($word , $replacement , $content , $count);
                //Convert into Smaller Case
                $str_content = strtolower($content);
                $str_word = strtolower($word);


                //Get the position of the word
                $position = strpos($str_content, $str_word , 0); 
                //Get the Exact word with case sensitive 
                $match_string = substr ($content , $position , strlen($word) );

                //Prepare Replacement
                $replacement = '<a href="'.$term_link.'">'.$match_string.'</a>';


                if(!empty($position)){
                    //Replace The word
                    $content = substr_replace($content, $replacement, $position, strlen($word));
                }

            }
        }

        return $content;
    }

I would like to replace few words in the content of the posts. For example.

Original:

Here is my test string. 
Which Need to Replace First Test word in the string.

Excepted String:

Here is my <a href="link"> test </a> string. 
Which Need to Replace First Test word in the string.

I have tried but there are few scenario where i have faced issues.

When that word has predefined link along with a word. Like

Here is my <a href="test case link"> test Case </a> string. 
Which Need to Replace First Test word in the string.

So my function exploded it in tow words like

Here is my <a href="link"> test </a> <a href="test case link"> Case </a> string.
Which Need to Replace First Test word in the string.

== My Solution ====

add_filter('the_content' , 'update_content');

function update_content($content){
        if((is_singular()){
            $termsList = array(
                 'test' => '<a href="test">test</a>'
            );

            foreach($termsList as $word => $term_link){

                //$content = str_replace($word , $replacement , $content , $count);
                //Convert into Smaller Case
                $str_content = strtolower($content);
                $str_word = strtolower($word);


                //Get the position of the word
                $position = strpos($str_content, $str_word , 0); 
                //Get the Exact word with case sensitive 
                $match_string = substr ($content , $position , strlen($word) );

                //Prepare Replacement
                $replacement = '<a href="'.$term_link.'">'.$match_string.'</a>';


                if(!empty($position)){
                    //Replace The word
                    $content = substr_replace($content, $replacement, $position, strlen($word));
                }

            }
        }

        return $content;
    }
Share Improve this question edited Oct 23, 2018 at 10:16 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 23, 2018 at 9:46 Ashish JangraAshish Jangra 1 4
  • What is your question? The solution you propose in your question seems to lead to a nested link, which will not work. – cjbj Commented Oct 23, 2018 at 11:04
  • I have set of words with their internal link. So what i want when blog content render that if any words match from the set of the words then first match word will replace with hyper link. – Ashish Jangra Commented Oct 24, 2018 at 5:20
  • I understand that. You are using the right filter and approach to do this. There seems to be a small coding error, but that's all. – cjbj Commented Oct 24, 2018 at 9:03
  • Yes that what i am not able to understand .. Here is my output $words_set = array('test'=> 'google.com'); $str = 'This is my test study. Read full test case'; After Using My Function: $str = 'This is my <a href="google.com">test</a> study. Read full test case'; BUT if the link is already exist in the string then my function didn't work properly i.e – Ashish Jangra Commented Oct 24, 2018 at 12:37
Add a comment  | 

1 Answer 1

Reset to default 0

This is a fairly complex PHP (rather than WP) issue if you want to take into account all possibilities.

If you replace test with <a href="#">test</a> you will run into problems if the content already has <a href="?">test</a>, because the result will then be <a href="?"><a href="#">test</a></a>

So, before you do the replace you will need to see if test is already a link. This is not that simple, because you cannot simply scan for <a href="...">test</a>. After all, you may have something like <a href="...">this is my test, yeah!. Or test may be a substring of another word, like protesting. To prevent the latter being targeted you might check for test, but then you may miss test,.

Another thing to consider: if there already is a link around test, do you want to keep that one or replace it?

So, depending on you needs you will have to build a regular expression that does complex pattern recognition on your content to make sure it does exactly what you need. For help with regular expressions, try Stack Overflow.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264371a963.html

最新回复(0)