1

In follow-up to my previous question, I want to replace every instance of an ALL-CAPS* word with a link of the following format:

dictionary.com/browse/<TERM>

The preg_replace call I am using is this:

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

Using http://gskinner.com/RegExr, it appears I have my regex correct, and that it should be replacing on each find.

Have I done something wrong, either in the preg_replace call, or pehaps in the registration of the plugin/filter to the Wordpress API?


Full context of the call:

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

add_filter('the_content', 'define_filter');

* I'm using the [A-Z][A-Z]+ syntax to ensure I do not match words like "I" and "A"

Community
  • 1
  • 1
warren
  • 32,620
  • 21
  • 85
  • 124

1 Answers1

2

I believe the function needs to return the result of the replacement:

return $content;

Also, that regex doesn't look right. If you want to match a whole word in all caps, it's

'#\b[A-Z]+\b#'

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

warren
  • 32,620
  • 21
  • 85
  • 124
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • the `\b` additions helped .. now it's substituting the link, but not appending the `$1` form the match.. any further thoughts perchance? – warren Aug 10 '10 at 01:59
  • I didn't notice that: you want `$0` (the whole match), not `$1` (the first capture group, which your regex doesn't have). – Alan Moore Aug 10 '10 at 02:26