-2

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

P.Membas
  • 33
  • 6
  • 2
    Sure .. but you're not going to get proper looking city names, ex: `"New york"`? – Halcyon Dec 22 '17 at 14:58
  • 1
    `Rio de janeiro`, `La paz`, `N'djamena`, `Addis ababa`, `Port au prince`, `Kuwait city`, `Kuala lumpur`, `Port of spain` – Mark Baker Dec 22 '17 at 15:01

3 Answers3

1

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

Harvey Fletcher
  • 1,167
  • 1
  • 9
  • 22
1

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);
P.Membas
  • 33
  • 6
0

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);
dotkomm
  • 11
  • 5