PHP remove all occurrences of string except last with preg_replace

While trying to manipulate a string in PHP, I ran into a simple but still challenging (for me), road block trying to remove all instances of “.” and replace with a comma with the exception of the last instance of the period.

It’s easy to select the first occurrence, simply add an integer argument at the end of the function to show how occurrences you want to replace.

$theContent = preg_replace('/\./', ',', $theContent, 1);

Simple! The 1 tells the function I want to replace up to the first occurrence.

Simple, if you know you will 4 occurrences, simple add a 3. However if you are doing this dynamically you need to know how many times the period occurs and subtract 1. So I did this:

$theCount = substr_count($theContent, '.') - 1; // count how many time the period occurs in the string
$theContent = preg_replace('/\./', ',', $theContent, $theCount); // replace all but last one

Great, but it’s still two lines of code for one simple process. So…

$theContent = preg_replace('/\./', ',', $theContent, (substr_count($theContent, '.') - 1));

Bam, done!

MailCatcher Suddenly Not Working In Vagrant

Mailcatcher fails enough for me to be a pain, but not frequently enough to remember what the fix is. It is enough to bring the development process to a screeching halt until I google the correct search string to bring me the results I am looking for. Which bring me to this post, a simple little fix here so I can find it right away and not have my day turned upside down.

Simply ssh into Vagrant

$ vagrant ssh

Get the process ID

$ ps ax | grep mailcatcher

Kill the process

$ sudo kill -9 (Process id)

Reset ip address

$ mailcatcher --ip=0.0.0.0

You should now be able to access MailCatcher via http://127.0.0.1:1080. You may have to “vagrant halt” and “vagrant up” for SMTP to work.

Developing, Design, and Content vs. Product

When ever given the choice to work on design or content, any designer chooses design. However front end design requires content to be able to get a true feel for what you are designing for. My conundrum is often that I have little to no content from clients to work with in the beginning stages, and I have the writing skills that would make an english major scream and throw windmill punches at the monitor until my content went away.

In house, I keep building things for The Animal House, with the expectations of the staff providing content to fill. Then the project never gets finished, because there is no content coming in to turn out a finished product. One can say that in house that is management problem on my end, and I did agree with that assessment, but I am beginning to look differently at this. If I can’t get my employees excited about something, then how on earth can I get an end user excited about product. The product concept itself has to have a well thought out usage validity, then the product has to be developed around the concept of the product with the user interaction in mind, and then design has to highlight the end product to enhance the user experience. (English majors, commence windmill punching…)

In short, which is much too late, after all that