0

I have a string -

 $file_data = This is a sample text. This text will be used as a dummy text for various RegEx operations using PHP.

I want to replace every 4 letter word with *

The Regex I'm using is

preg_replace("/\w{4}/", "*", $file_data)

But this also replaces words with length >=4. I want it to be strictly 4. How should I do it?

Siddharth Thevaril
  • 3,722
  • 3
  • 35
  • 71
  • Maybe a useful resource: On [Regexr](http://regexr.com/) you can fiddle around and validate your regex realltime. (Holy... sorry for that previous link... I thought it was .org o.o) – Xyv Aug 27 '15 at 07:39

1 Answers1

2

Try using a regular expression such as:

\b[a-zA-Z]{4}\b

The \bs ensure that the beginning and end of the four letter pattern [a-zA-Z]{4} are word boundaries.

Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29