Monthly Archives: 2月 2015

「php学習」json_encode()を利用するサンプルコード

phpコード:
<?php
$a = array(‘<foo>’,”‘bar'”,'”baz”‘,’&blong&’, “\xc3\xa9”);
echo “Normal: “, json_encode($a), “\n”;
echo “Tags: “, json_encode($a, JSON_HEX_TAG), “\n”;
echo “Apos: “, json_encode($a, JSON_HEX_APOS), “\n”;
echo “Quot: “, json_encode($a, JSON_HEX_QUOT), “\n”;
echo “Amp: “, json_encode($a, JSON_HEX_AMP), “\n”;
echo “Unicode: “, json_encode($a, JSON_UNESCAPED_UNICODE), “\n”;
echo “All: “, json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), “\n\n”;

$b = array();

echo “Empty array output as array: “, json_encode($b), “\n”;
echo “Empty array output as object: “, json_encode($b, JSON_FORCE_OBJECT), “\n\n”;

$c = array(array(1,2,3));

echo “Non-associative array output as array: “, json_encode($c), “\n”;
echo “Non-associative array output as object: “, json_encode($c, JSON_FORCE_OBJECT), “\n\n”;

$d = array(‘foo’ => ‘bar’, ‘baz’ => ‘long’);

echo “Associative array always output as object: “, json_encode($d), “\n”;
echo “Associative array always output as object: “, json_encode($d, JSON_FORCE_OBJECT), “\n\n”;
?>

結果:
Normal: [“<foo>”,”‘bar'”,”\”baz\””,”&blong&”,”\u00e9″]
Tags: [“\u003Cfoo\u003E”,”‘bar'”,”\”baz\””,”&blong&”,”\u00e9″]
Apos: [“<foo>”,”\u0027bar\u0027″,”\”baz\””,”&blong&”,”\u00e9″]
Quot: [“<foo>”,”‘bar'”,”\u0022baz\u0022″,”&blong&”,”\u00e9″]
Amp: [“<foo>”,”‘bar'”,”\”baz\””,”\u0026blong\u0026″,”\u00e9″]
Unicode: [“<foo>”,”‘bar'”,”\”baz\””,”&blong&”,”é”]
All: [“\u003Cfoo\u003E”,”\u0027bar\u0027″,”\u0022baz\u0022″,”\u0026blong\u0026″,”é”]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {“0”:{“0″:1,”1″:2,”2”:3}}

Associative array always output as object: {“foo”:”bar”,”baz”:”long”}
Associative array always output as object: {“foo”:”bar”,”baz”:”long”}

SELinuxを無効化する方法

修正方法
#vim /etc/selinux/config
修正前 ”SELINUX=enforcing”
修正後  ”SELINUX=disabled”

selinuxステータスの確認
操作コマンド:
/usr/bin/setstatus -v
詳細:
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: enforcing
Policy version: 21
Policy from config file: targeted
getenforce/setenforce

「JavaScript学習」jqueryでノードを削除するサンプルコード

参考コード:
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>jqueryでノードを削除_www.arkgame.com</title>
<script type=”text/javascript” src=”jquery-1.7.2.js”></script>
<script type=”text/javascript”>
//ノード移動
$(function(){
//var removeLi = $(“ul li:eq(3)”).remove();
//removeLi.appendTo($(“ul”));
//$(“ul li”).remove(“li[title != 2]”);

//要素の内容をクリア
$(“ul li:eq(3)”).empty();
});
</script>
</head>
<body>
<p title=”study in startnews24″>arkgame.com</p>
<ul>
<li title=”1″>ゲーム</li>
<li title=”2″>アプリ</li>
<li title=”3″>IT技術</li>
<li title=”4″>ソフト</li>
<li title=”5″>学習</li>
<li title=”6″>取得</li>
</ul>
</body>
</html>

「php初心者」UnserializeとAutoloadを利用するサンプルコード

例1–unserialize関数
<?php
$string = ‘O:6:“Foostartnews24”:2:{s:3:“foo”;s:1:“1”;s:3:“startnews24”;s:1:“2”;}’;
$result = unserialize($string);
var_dump($result);
/*
object(__PHP_Incomplete_Class)[1]
public ‘__PHP_Incomplete_Class_Name’ => string ‘Foostartnews24’ (length=6)
public ‘foo’ => string ‘1’ (length=1)
public ‘startnews24’ => string ‘2’ (length=1)
*/
?>

例2—spl_autoload_register関数
<?php
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});
$string = ‘O:6:“Foostartnews24”:2:{s:3:“foo”;s:1:“1”;s:3:“startnews24”;s:1:“2”;}’;
$result = (array)unserialize($string);
var_dump($result);
?>

例3—FAKEクラスを利用
<?php
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});

$string = ‘O:6:“Foostartnews24”:2:{s:3:“foo”;s:1:“1”;s:3:“startnews24”;s:1:“2”;}’;
$functions = spl_autoload_functions();
foreach ($functions as $function) {
spl_autoload_unregister($function);
}
$result = (array)unserialize($string);
foreach ($functions as $function) {
spl_autoload_register($function);
}
var_dump($result);
?>

「php開発」splitとexplodeの使い方のまとめ

関数説明:
array split (string $pattern, string $string [, int $limit])
array explode ( string $separator, string $string [, int $limit])

例1—explode
<?php
$test_startnews24 = end(explode(‘.’, ‘abc_startnews24.txt’));
echo $test_startnews24;//output txt

例2—split
<?php
$test_startnews241 = end(split(‘\.’,’abc_startnews24.txt’));
echo $test_startnews241;//output txt

Centosにyumでphpをアップデートする方法

操作コマンド
rpm –import http://repo.webtatic.com/yum/RPM-GPG-KEY-webtatic-andy
wget -P /etc/yum.repos.d/ http://repo.webtatic.com/yum/webtatic.repo
yum –enablerepo=webtatic update php

PHPの削除パラメータunset()とnullを利用する方法

1.目的:
特定のかさばる変数(配列やオブジェクト)を使用しない場合、削除関数を利用する

2.実現方法
方法1 $varname=null;
方法2 unset($varname);

3.PHPソースコード
‘a’,
‘b’ => ‘b’
);
$b = array(
‘a’ => ‘a’,
‘b’ => ‘b’
);
$a[‘b’] = null;
unset($b[‘b’]);
print(‘

‘);
?>

4.検証結果:
Array
(
[a] => a
[b] =>
)
Array
(
[a] => a
)

PHP開発でget_magic_quotes_gpc関数を利用するソースコード

PHPコード:
<html>
<!–POSTで単一引用符号を渡す –>
<body>
<form action=”first.php” method=”post”>
<input type=”text” name=”startnews24_lastname” value=”startnews24’it and game”>
<input type=”submit” value=”確認”>
</form>
</body>
</html>
<?php
echo get_magic_quotes_gpc(); // 0
echo $_POST[‘startnews24_lastname’]; // startnews24’it and game
echo addslashes($_POST[‘startnews24_lastname’]); // startnews24\’it and game

if (!get_magic_quotes_gpc()) {
$startnews24_lastname = addslashes($_POST[‘startnews24_lastname’]);
} else {
$startnews24_lastname = $_POST[‘startnews24_lastname’];
}

echo $startnews24_lastname; // startnews24\’it and game
$sql = “INSERT INTO startnews24_lastnames (startnews24_lastname) VALUES (‘$startnews24_lastname’)”;
?>

「Android学習」ListScrollView、ScrollViewの使い方を利用するサンプルコード

Javaコード:
public class ListScrollView extends ScrollView {
private List list = new ArrayList();
private int scrollPaddingTop;
private int scrollPaddingLeft;
private int[] scrollLoaction = new int[2];
private final static int UPGLIDE = 0;
private final static int DOWNGLIDE = 1;
private int glideState;
public ListScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private int downY = 0;
private int moveY = 0;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = (int) ev.getY();
//System.out.println(“actiondown” + ev.getY());
break;
case MotionEvent.ACTION_MOVE:
moveY= (int) ev.getY();
//System.out.println(“move” + moveY + “down” + downY);
if((moveY – downY) >= 0) {
//System.out.println(“””””’DOWNGLIDE”””””'”);
glideState = DOWNGLIDE;
} else {
glideState = UPGLIDE;
}
break;
case MotionEvent.ACTION_UP:
default:
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int x = (int) ev.getX() + scrollLoaction[0];
int y = (int) ev.getY() + scrollLoaction[1];
for (int i = 0; i < list.size(); i++) {
ListView listView = list.get(i);
int[] location = new int[2];
listView.getLocationInWindow(location);
int width = listView.getWidth();
int height = listView.getHeight();

if (x >= location[0] + scrollPaddingLeft
&& x <= location[0] + scrollPaddingLeft + width
&& y >= location[1] + scrollPaddingTop
&& y <= location[1] + scrollPaddingTop + height) {
//System.out.println(glideState);
if(( (listView.getLastVisiblePosition() == (listView.getCount()-1)) && (glideState == UPGLIDE) ) ) {
//System.out.println(“up”);
break;
}
if(( (listView.getFirstVisiblePosition() == 0) && (glideState == DOWNGLIDE))) {
//System.out.println(“down”);
break;
}
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(ev);
}
private void findAllListView(View view) {
if (view instanceof ViewGroup) {
int count = ((ViewGroup) view).getChildCount();
for (int i = 0; i < count; i++) {
if (!(view instanceof ListView)) {
findAllListView(((ViewGroup) view).getChildAt(i));
}
}
if (view instanceof ListView) {
list.add((ListView) view);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
scrollPaddingTop = getTop();
scrollPaddingLeft = getLeft();
getLocationInWindow(scrollLoaction);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (this.getChildCount() != 1) {
try {
throw new ScrollException();
} catch (ScrollException e) {
e.printStackTrace();
}
}
list.clear();
findAllListView(this.getChildAt(0));
}
}

「Android初心者」ListViewの高さを計算するソースコード

サンプルコード:
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() – 1));
listView.setLayoutParams(params);
}
}