Friday, March 10, 2006

Using ltrim is not the right answer sometimes.

In a PHP application I was debugging today for a company I found them using ltrim to remove a substring from the start of a string which was producing weird results such as "tal" after invoking with "Total".

When I asked their programmer why is he using ltrim, he replied to strip out a substring from the start of the string.

His code was:

$clean=ltrim($unclean, "To");



So what's the problem? If a string starts with To such as in "ToTotal" (hypothetically), it incorrectly returns "tal" when the needed result was "Total" with just the first part of To stripped.

I've posted the correct way to strip a substring using regular expressions.
  post to Del.icio.us

eXTReMe Tracker