Monthly Archives: 4月 2017

「PHP」文字列からurlを抽出するコード

phpコード
<?php
function pageLinks($html, $current_url = “”, $repath = false){
preg_match_all(“/\<a.+?href=(\”|’)(?!javascript:|#)(.+?)(\”|’)/i”, $html, $matches);
$links = array();
if(isset($matches[2])){
$links = $matches[2];
}
if($repath && count($links) > 0 && strlen($current_url) > 0){
$pathi = pathinfo($current_url);
$dir = $pathi[“dirname”];
$base = parse_url($current_url);
$split_path = explode(“/”, $dir);
$url = “”;
foreach($links as $k => $link){
if(preg_match(“/^\.\./”, $link)){
$total = substr_count($link, “../”);
for($i = 0; $i < $total; $i++){
array_pop($split_path);
}
$url = implode(“/”, $split_path) . “/” . str_replace(“../”, “”, $link);
}elseif(preg_match(“/^\/\//”, $link)){
$url = $base[“scheme”] . “:” . $link;
}elseif(preg_match(“/^\/|^.\//”, $link)){
$url = $base[“scheme”] . “://” . $base[“host”] . $link;
}elseif(preg_match(“/^[a-zA-Z0-9]/”, $link)){
if(preg_match(“/^http/”, $link)){
$url = $link;
}else{
$url = $dir . “/” . $link;
}
}
$links[$k] = $url;
}
}
return $links;
}
header(“content-type: text/plain”);
$url = “http://www.sample.com”;
$html = file_get_contents($url);
// Gets links from the page:
print_r(pageLinks($html));
// Gets links from the page and formats them to a full valid url:
print_r(pageLinks($html, $url, true));

「PHP」utf8文字列の一部を取得するコード

PHPコード
<?php
function utf8Substr($str, $from, $len)
{
return preg_replace(‘#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,’.$from.’}’.
‘((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,’.$len.’}).*#s’,
‘$1’,$str);
}
?>

「PHP」base64_encode()でURLをエンコードする

1.エンコード
function urlsafe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array(‘+’,’/’,’=’),array(‘-‘,’_’,”),$data);
return $data;
}

2.デコード
function urlsafe_b64decode($string) {
$data = str_replace(array(‘-‘,’_’),array(‘+’,’/’),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr(‘====’, $mod4);
}
return base64_decode($data);
}

「PHP」文字列がutf8エンコードかどうか判断するコード

PHPコード:
function CheckUtf8($str) {
return preg_match(‘%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs’, $str);
}

「PHP開発」画像からASCIIへ変換するサンプルコード

PHPコード
<?php
$image = ‘info.jpg’;
// Supports http if allow_url_fopen is enabled
$image = file_get_contents($image);
$img = imagecreatefromstring($image);
$width = imagesx($img);
$height = imagesy($img);
for($h=0;$h<$height;$h++){
for($w=0;$w<=$width;$w++){
$rgb = imagecolorat($img, $w, $h);
$a = ($rgb >> 24) & 0xFF;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$a = abs(($a / 127) – 1);
if($w == $width){
echo ‘<br>’;
}else{
echo ‘<span style=”color:rgba(‘.$r.’,’.$g.’,’.$b.’,’.$a.’);”>#</span>’;
}
}
}
?>

Ubuntuにおけるtomcat起動スクリプトコード

#cd /etc/rc.d/init.d/
#vi tomcat

サンプルコード
#!/bin/sh
#
# tomcat: Start/Stop/Restart tomcat
#
# chkconfig: 2345 80 20
# description: Tomcat is a Java Servlet Container
#
#
# match these values to your environment:
export CATALINA_BASE=/usr/local/tomcat
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_TMPDIR=/usr/local/tomcat/temp
export JRE_HOME=/usr/java/jdk15

# Source function library.
. /etc/rc.d/init.d/functions

TOMCAT=/usr/local/tomcat

start() {
echo -n $”Starting Tomcat: ”
$TOMCAT/bin/catalina.sh start
}

stop() {
echo -n $”Stopping Tomcat: ”
$TOMCAT/bin/catalina.sh stop

}

# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
;;
status)
;;
restart)
stop
start
;;
*)
echo $”Usage: $0 {start|stop|restart}”
;;
esac
exit $RETVAL

権限を設定
#chmod a+x tomcat
#chkconfig –add tomcat
#chkconfig –list tomcat

Ubuntu 16.04にApache Tomcatをインストールするメモ

1.Tomcatのインストール
#get install tomcat8
#apt-get install tomcat8-docs tomcat8-examples tomcat8-admin
#systemctl start tomcat8
#systemctl stop tomcat8
URL確認
http://example.com:8080/

2.tomcat-users.xmlの設定
/var/lib/tomcat8/conf/tomcat-users.xml
<role rolename=”manager-gui”/>
<role rolename=”admin-gui”/>
<user username=”username” password=”password” roles=”manager-gui,admin-gui”/>
tomcat8再起動
#systemctl restart tomcat8

「javaScript入門」window.onscrollで要素のスクロールを行うコード

javascriptコード:
var sign = 80;
window.onscroll = window.onresize = function(){
var oScrollTop=$(window).scrollTop();
if ( oScrollTop > 80) {
//write some code
}
if ( oScrollTop < 80) {
//write some code
}
if ( oScrollTop > sign) {
sign = oScrollTop;//scrollTop更新
//console.log(‘↓’);
}
if ( oScrollTop< sign) {
sign = oScrollTop//scrollTop更新
//console.log(‘↑’);
}
}

「Spring MVC」 HandlerMethodReturnValueHandlerの使い方

Javaコード

package org.springframework.web.method.support;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;

public interface HandlerMethodReturnValueHandler {
boolean supportsReturnType(MethodParameter returnType);

void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;

}

「Spring MVC」 Jacksonで@JsonFilterのサンプルコード

Javaコード
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

public class CustJsonSerializer {
static final String DYNC_INCLUDE = “DYNC_INCLUDE”;
static final String DYNC_FILTER = “DYNC_FILTER”;
ObjectMapper mapper = new ObjectMapper();

@JsonFilter(DYNC_FILTER)
interface DynamicFilter {
}

@JsonFilter(DYNC_INCLUDE)
interface DynamicInclude {
}

/**
* @param clazz
* @param include
* @param filter
*/
public void filter(Class<?> clazz, String include, String filter) {
if (clazz == null) return;
if (include != null && include.length() > 0) {
mapper.setFilterProvider(new SimpleFilterProvider().addFilter(DYNC_INCLUDE,
SimpleBeanPropertyFilter.filterOutAllExcept(include.split(“,”))));
mapper.addMixIn(clazz, DynamicInclude.class);
} else if (filter !=null && filter.length() > 0) {
mapper.setFilterProvider(new SimpleFilterProvider().addFilter(DYNC_FILTER,
SimpleBeanPropertyFilter.serializeAllExcept(filter.split(“,”))));
mapper.addMixIn(clazz, DynamicFilter.class);
}
}

public String toJson(Object object) throws JsonProcessingException {
return mapper.writeValueAsString(object);
}
}

class SampleJson {
public void main(String args[]) {
CustJsonSerializer cft= new CustJsonSerializer();
cft.filter(Article.class, “id,name”, null);
String include = cft.toJson(new Article());

cft= new CustJsonSerializer();
cft.filter(Article.class, null, “id,name”);

String filter = cft.toJson(new Article());

System.out.println(“message include: ” + include);
System.out.println(“message filter: ” + filter);
}
}

1 / 1312345...10...最後 »