P.Membas
P.Membas

Reputation: 33

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. :-)

Upvotes: -2

Views: 273

Answers (3)

P.Membas
P.Membas

Reputation: 33

Thanks, @dotkomm and @Harvey Fletcher it was a combination of your answeres that fixed the issue.

Solution looks like this:

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

Upvotes: 1

dotkomm
dotkomm

Reputation: 11

There are several Methods, the most elegant way in my opinion would be doing it with css text-transform property capitalize text-transform: capitalize; as it is not altering the data

But yes ucfirst() should also do the job, but here the data would be altered for further processing

Also you could do the job inside the mysql query

In your example, if you want to use ucfirst() you should replace the line

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

with

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

Upvotes: 0

Harvey Fletcher
Harvey Fletcher

Reputation: 1172

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

Upvotes: 1

Related Questions