Take the 2-minute tour ×

I needed to write a custom module in drupal to help out with my location search. Initially I simply needed to remove a comma from queries, and then I realized that I would need to replace all instances of states with their abbreviation (California -> CA) because of how information is stored in my database. However, upon doing this I found out that my method of using preg_replace seems to be dependent on upper/lowercase. So in this line:

$form_state['values'] = preg_replace("/alabama/", 'al', $form_state['values']);

"alabama" will be replaced with "al", but "Alabama" or "ALABAMA" will not. Is there a way to replace any instance of Alabama with its abbreviation without accounting for every possible variation in casings?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted
$form_state['values'] = preg_replace("/alabama/i", 'al', $form_state['values']);

The 'i' modifier will make the pattern case-insensitive.

share|improve this answer
Awesome, thanks so much. Insensitive was the word I couldn't remember, which apparently made my answer impossible to find. Thanks a bunch. –  Mrweiner Jun 21 '12 at 7:18
add comment

you can try also str_ireplace() it's Case-insensitive

$str = 'alabama ,Alabama,ALABAMA';
$replace = str_ireplace('alabama','al',$str);
echo $str;
echo "<br/>";
echo $test;
share|improve this answer
add comment

Your Answer


