'Captilizing particular words in PHP string
I would like to take a string and seek out specific words and capitalize them. So for a string like "Lorem ipsum dolor sit amet, consectetur adipisicing elit.", I want to be able to only captalize ipsum and adipisicing. So basically put a capitalization filter on certain words.
How can this be done?
Solution 1:[1]
Don't use regex for this, str_replace is the clearest, easiest way:
$str = str_replace(
array('ipsum','adipisicing'),
array('Ipsum','Adipisicing'),
'Lorem ipsum dolor sit amet, consectetur adipisicing elit.'
);
Or a bit more clever:
$words = array('ipsum', 'adipisicing');
$str = str_replace($words, array_map('ucfirst', $words), 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.');
Solution 2:[2]
$capitalize_me = array('word1','word2'....'wordn');
$capitalized = array_map('ucfirst',$capitalize_me);
$new_sentence = str_replace($capitalize_me,$capitalized,$old_sentence);
not the most efficient, but does the work
Solution 3:[3]
There are too many ways to do this...
- preg_replace
- str_replace
- implode string into an array and iterate through with
str_replace, then explode array back into a string
Solution 4:[4]
$str = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';
$caps = 'ipsum|adipisicing';
echo preg_replace_callback(
'/(^|\s)('.$caps.')(\s|$)/'
, function($m){ return $m[1].ucfirst($m[2]).$m[3]; }
, $str
);
That regexp could probably be better (I'm thinking you could use \W), but if you don't account for whitespace before/after the word, it will replace in the middle of words too.
Solution 5:[5]
This works well for me:
<?php
$word_array = array('ipsum', 'adipisicing');
$soucre_sentence = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';
$captilize_words = array();
$replacements_captalized = array();
$replacements_all_caps = array();
foreach ($word_array as $word) {
$captilize_words[] = '/\b' . $word . '\b/i';
$replacements_captalized[] = ucfirst(strtolower($word));
$replacements_all_caps[] = strtoupper($word);
}
$ret = preg_replace($captilize_words, $replacements_all_caps, $soucre_sentence);
echo $ret;
?>
Added capitalized and ALL CAPS replacements since I am not clear on what you really want your output to be. Choose one!
EDIT: Genuinely did not know about the flexibility of array_map (that @mmattax) and reworked it one last time with that tip.
<?php
$word_array = array('ipsum', 'adipisicing');
$soucre_sentence = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';
$captilize_words = array_map(function ($word) { return '/\b' . $word . '\b/i'; }, $word_array);
$replacements_captalized = array_map('ucfirst', $word_array);
$replacements_all_caps = array_map('strtoupper', $word_array);
$ret = preg_replace($captilize_words, $replacements_all_caps, $soucre_sentence);
echo $ret;
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | mayersdesign |
| Solution 3 | Ian Atkin |
| Solution 4 | mcovey |
| Solution 5 |
