Generate regular expression based on inputted number
One thing I would do is that str_replace can receive an array of items to look for and an array of its replacement:

PHP str_replace

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);


instead of going line by line to do it.

Just nit-picking but 

sizeof() is an alias of count() and count() is more widely used, so use something that is more general in the PHP community

Also $_POST != null would typically be !isset($_POST) rather as explained in this post