Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active May 10, 2016
Embed
What would you like to do?
【CodeIgniter3】ガラケーの時だけ文字コードをShift_JISに変更する方法
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->_display('welcome_message');
}
private function _display($view, $vars = [])
{
$output = $this->load->view($view, $vars, TRUE);
if($this->_get_device_type() == 'featurephone'){
$this->output->set_header('Content-Type: text/html; charset=Shift_JIS');
$output = mb_convert_encoding($output, "SJIS", "UTF-8");
}
$this->output->set_output($output);
}
private function _get_device_type()
{
$this->load->library(['user_agent']);
if(
strpos($this->agent->agent_string(), 'DoCoMo') !== FALSE ||
strpos($this->agent->agent_string(), 'KDDI') !== FALSE ||
strpos($this->agent->agent_string(), 'Vodafone') !== FALSE ||
strpos($this->agent->agent_string(), 'SoftBank') !== FALSE ||
strpos($this->agent->agent_string(), 'DDIPOKET') !== FALSE ||
strpos($this->agent->agent_string(), 'UP.Browser') !== FALSE ||
strpos($this->agent->agent_string(), 'WILLCOM') !== FALSE ||
strpos($this->agent->agent_string(), 'J-PHONE') !== FALSE ||
strpos($this->agent->agent_string(), 'emobile') !== FALSE
)
{
return "featurephone";
}
else if ($this->agent->is_mobile())
{
return "smartphone";
}
else
{
return "pc";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment