phpucfirst

Capitalize city name from database with ucfirst in php?


is it possible to make the {CITY} output to be capitalized with for example ucfirst? Can this code be altered in some way to make it capitalized?

<?php   
$city_meta = get_option('city_meta' );
$city_description = get_option('city_description' );
$default_api = get_option('default_api' );

/* Update Settings */
if(isset($_POST['submit_changes'])){ 
    update_option('city_meta',$_POST['city_meta']);
    update_option('city_description',$_POST['city_description']);

    $info =weatherCity::getCities();
    //$info =array_slice($info, 0, 3);  
    $sr=0;
    foreach($info as $key=>$value):
        $postTitle = 'Väder '.$value->name;
        global $wpdb;
        $pageInfo=$wpdb->get_row("SELECT ID FROM `".$wpdb->prefix."posts` WHERE post_title = '" . $postTitle . "' AND `post_type`='page' ");

        if(!empty($pageInfo)):  

            $metaTitle=$_POST['city_meta'];
            $metaDesc=$_POST['city_description'];

            $metaTitle=str_replace('{CITY}',$value->name,$metaTitle);
            $metaDesc=str_replace('{CITY}',$value->name,$metaDesc);

            update_post_meta($pageInfo->ID,'_yoast_wpseo_title',$metaTitle);
            update_post_meta($pageInfo->ID,'_yoast_wpseo_metadesc',$metaDesc);

            $sr++;
        endif;
    endforeach;



    set_error_message( _e('City Meta Settings has been updated succesfully','weather'),'0');
    foreceRedirect(admin_url('admin.php?page=city_meta') );
    exit;
}

?>

Would be great if someone could find a simple solution for this. :-)


Solution

  • You can use ucwords()

    $metaTitle=ucwords(str_replace('{CITY}',$value->name,$metaTitle));
    $metaDesc=ucwords(str_replace('{CITY}',$value->name,$metaDesc));
    

    This will uppercase the first character of each word in a string.

    PHP.Net link

    http://php.net/manual/en/function.ucwords.php