preg_replace or regex string translation

Posted by ccolon on Stack Overflow See other posts from Stack Overflow or by ccolon
Published on 2012-10-27T22:28:32Z Indexed on 2012/10/27 23:00 UTC
Read the original article Hit count: 154

Filed under:
|
|

I found some partial help but cannot seem to fully accomplish what I need. I need to be able to do the following:

I need an regular expression to replace any 1 to 3 character words between two words that are longer than 3 characters with a match any expression:

For example: walk to the beach ==> walk(.*)beach

If the 1 to 3 character word is not preceded by a word that's longer than 3 characters then I want to translate that 1 to 3 letter word to ' ?'

For example: on the beach ==> on ?the ?beach

The simpler the rule the better (of course, if there's an alternative more complicated version that's more performant then I'll take that as well as I eventually anticipate heavy usage eventually).

This will be used in a PHP context most likely with preg_replace. Thus, if you can put it in that context then even better!

© Stack Overflow or respective owner

Related posts about php

Related posts about regex