0

$personInfo['mantra'] is the piece that I'm working with. I want everything to be capitalized, with the exception of prepositions and articles (e.g. 'a', 'an', 'the', etc.) Is there an efficient way to do this? I'm doing this as ucwords(strtolower($personInfo['mantra'])) so far, but obviously this does not take into account the prepositions and articles remaining lowercase.

A mantra is all capitalized (e.g. 'I LIVE THE WAY I WANT') in the database, and there are thousands, so it is not practical to manually change them. To provide an example, I would like the sentence: 'I LIVE THE WAY I WANT' to become 'I Live the Way I Want'.

4 Answers 4

3

This doesn't seem an easy task for regex. As there's not many that prepositions and articles, can't you just use a straight str_replace after ucwords() ?

$personInfo['mantra'] = ucwords(strtolower($personInfo['mantra']));
$personInfo['mantra'] = " " . $personInfo['mantra'] . " ";
$personInfo['mantra'] = str_replace(
  array(" An ", " A ", " The "), 
  array(" an ", " a ", " the "), 
$personInfo['mantra']);
$personInfo['mantra'] = trim($personInfo['mantra']);

Adding spaces to the beginning and end of each 'mantra' helps to make sure str_replace can replace properly.

You could simplify it by putting all needed prepositions and articles in an array, looping through it and doing it a str_replace on each one. A regex alternative using preg_replace_callback would be possible if needed, but I think it would be both slower and more complex.

Sign up to request clarification or add additional context in comments.

2 Comments

Sounds like this is probably gonna be the best case, I'll accept it after timer expires. :)
str_replace() supports arrays: str_replace( array( ' A ', ' An ', ' The ' ), array( ' a ', ' an ', ' the ' ), $personInfo[ 'mantra' ] ) (also notice that the words are not all uppercase anymore at this point).
1

Here's kind of a slower, brute-force way to handle it. Not very elegant, but it would get the job done:

<?php

$articles = array("a","an","the");
$prepositions = array("under","over","beside",etc...);
foreach($mantras as $key => $mantra) {
    $words = explode(" ", strtolower($mantra));
    $newmantra = "";
    foreach($words as $word) {
        if (!in_array($word, $articles) && !in_array($word, $prepositions)) {
            $newmantra .= ucfirst($word)." ";
        } else {
            $newmantra .= $word." ";
        }
    }
    $mantras[$key] = rtrim($newmantra);
}

?>

Comments

0

I think you can go with your ucwords snippet first, and then use the replace function of your database system (which is ?) to search and replace prepositions.

Comments

0

my approach to this would be to define an array with prepositions, and upercase all the words except those.

But this would be more complex than a one liner.

regexp is no solution here i believe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.