Preg_replace does not replace Wordpress plugin

In response to the previous question, I want to replace each instance of the word ALL-CAPS * with a link in the following format:

dictionary.com/browse/<TERM> 

I use the preg_replace call:

 $content = preg_replace('#[AZ][AZ]+#', '<a href="//dictionary.com/browse/$1">$1</a>', $content); 

Using http://gskinner.com/RegExr , it looks like I have the correct regular expression and that it should be replaced with every find.

Did I do something wrong both in the preg_replace call and in the process of registering the plugin / filter in the Wordpress API?


Full call context:

 function define_filter($content){ $content = preg_replace('#[AZ][AZ]+#', '<a href="//dictionary.com/browse/$1">$1</a>', $content); } add_filter('the_content', 'define_filter'); 

* I use the syntax [AZ][AZ]+ to make sure that I do not match words like "I" and "A"

+1
source share
1 answer

I believe that the function should return the result of the replacement:

 return $content; 

In addition, this regular expression does not look right. If you want to combine whole words in all caps, this

 '#\b[AZ]+\b#' 

Also, you want $0 (the whole match), not $1 (the first capture group your regex doesn't exist)

+1
source

Source: https://habr.com/ru/post/1337970/


All Articles