ブルータス、お前もか?古代の暗号シーザー。 #phpcode

2023.04.30

Logging

おはようございます。古代にも暗号というものがあったらしい。古代の人が使っていたシーザーという暗号をPHP化しました。demo74のページを見ると実行結果が表示されていると思います。

古代にはPCというものが無かったので、これでも解読するのにある程度、時間がかかったんでしょうね。ぱっと見、暗号化されているのは分かるけど瞬時に解読できる人はあまりいなかっただと思います。近年では量子暗号とか、パッと見どころかPCがあっても鍵が無いと解読に途方も無い時間を費やする暗号までありますよね。そう思うと暗号の歴史を辿るのも面白いかもしれないですね。

<?php
function caesarCipher($str, $shift) {
  $result = "";
  $len = strlen($str);
  // 26文字のアルファベットを配列として定義する
  $alpha = range('a', 'z');
  
  for ($i = 0; $i < $len; $i++) {
    $char = strtolower($str[$i]); // 大文字を小文字に変換する
    if (in_array($char, $alpha)) { // アルファベットの場合のみシフトする
      $index = array_search($char, $alpha); // アルファベットの位置を検索する
      $newIndex = ($index + $shift) % 26; // シフト後のアルファベットの位置を計算する
      $result .= $alpha[$newIndex]; // シフト後のアルファベットを結果に追加する
    } else {
      $result .= $char; // アルファベット以外はそのまま結果に追加する
    }
  }
  return $result;
}

// 使用例
$plaintext = "hello world";
$ciphertext = caesarCipher($plaintext, 3);
echo $ciphertext; // "khoor zruog"

タグ

$alpha, $ciphertext, $len, $newIndex, $plaintext, $shift, caesarCipher, char, echo, else, lt, quot, range, result, return, STR, strlen, シーザー, 小文字, 解読,

WordPress自動日本語タグを吐き出しプラグインを作りました。

2018.11.17

Logging

WordPress自動日本語タグを吐き出しプラグインを作りました。
あのjapanese autotagというプラグインと考え方は同じですが、
自分が作ったものはその簡略化したものです。
ソースコードは全ったく違う感じですが、動作は似たような感じです。
機能はjapanese autotagよりかは少ないですが、これだけで十分かなと思います。
ダウンロードはこちらから
https://zip358.com/tool/jp-auto-tag.zip [v2に対応済み]
尚、Yahoo デベロッパーのアプリケーションIDが必要となります。
ソースコードは下記になります。※v1のソースコードなので今は動きません!!最新の記事を参照ください。

<?php
/*
Plugin Name: jp-auto-tag
Version: 0.1.11
Description: auto jp tag
Author: taoka toshiaki
Author URI: https://zip358.com/
Plugin URI: https://wordpress.org/extend/plugins/jp-auto-tag/
*/
class jp_auto_tag{
    public $db_option = "jp_auto_tag";
    //api
    public $results = "ma";
    public $filter = array("1","2","3","4","5","6","7","8","9","10","11","12","13");
    function frm_page(){
        add_menu_page('jp-auto-tag','jp-auto-tag',  'manage_options', __FILE__, array($this,'show_text_option_page'), '',8);
    }
    function show_text_option_page(){
        wp_enqueue_style( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css', array(), '3.3.6' );
        wp_enqueue_script( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js', array(), '3.3.6');
        $options = get_option($this->db_option);
        if(!empty($options)){
            $appid = $options["appid"];
            foreach ($this->filter as $key => $value) {
               if($options["filter".$value] == $value){
                    $f[] = "checked";
               }else{
                   $f[] = "";
               }
            }
        }
        include_once dirname( __FILE__ ).'/jp-auto-tag-tmp.php';
    }
    function ajax_event(){
        $appid = $_POST["appid"];
        $filter =$_POST["filter"];
        $options["appid"] = $appid;
        foreach ($this->filter as $key => $value) {
            if(in_array($value,$filter,true)){
                $options["filter".$value] = $value;
            }else{
                $options["filter".$value] = "";
            }
        }
        update_option($this->db_option, $options);
        $obj["appid"] = $appid;
        $obj["filter"] = $filter;
        print json_encode($obj);
        die(0);
    }
    function api_tag($post_id){
ini_set("display_errors",1);
        $post = get_post($post_id);
        $title = $post->post_title;
        $content = strip_tags($post->post_content);
        $sentence = $title.$content;
        if(strlen($sentence)>102400){
            $sentence = substr($sentence,0,102400);
        }
        $options = get_option($this->db_option);
        if(!empty($options)){
            $appid = $options["appid"];
            foreach ($this->filter as $key => $value) {
               if($options["filter".$value] == $value){
                    $f[] = $value;
               }
            }
        }
        if($appid){
            $filter = implode("|",$f);
            if(!$filter){
                $url = "https://jlp.yahooapis.jp/MAService/V1/parse?appid=$appid&results=$this->results&sentence=".urlencode($sentence);
            }else{
                $url = "https://jlp.yahooapis.jp/MAService/V1/parse?appid=$appid&results=$this->results&ma_filter=$filter&sentence=".urlencode($sentence);
            }
            $xml = @file_get_contents($url);
            $xml_obj = simplexml_load_string($xml);
            if($xml_obj->ma_result->word_list){
                foreach($xml_obj->ma_result->word_list->word as $word) {
                    if($word->surface){
                        $tags[] = $word->surface;
                    }
                    if(is_array($tags)){
                        wp_set_post_tags($post_id, implode(",",array_unique($tags)), false);
                    }
                }
            }
        }
    }
}
$jp_auto_tag = new jp_auto_tag();
add_action('save_post',array($jp_auto_tag,'api_tag'));
add_action('publish_post',array($jp_auto_tag,'api_tag'));
add_action('admin_menu', array($jp_auto_tag, 'frm_page'));
add_action('wp_ajax_ajax_event',array($jp_auto_tag,'ajax_event'));
<form id="ajax-frm">
<table class="table">
    <tr>
        <td>
            アプリケーションID
        </td>
        <td>
            <input type="text" name="appid" value="<?=$appid?>" class="form-control">
        </td>
    </tr>
    <tr>
        <td>
            解析結果として出力する品詞
        </td>
        <td>
            <input type="checkbox" name="filter[]" value="1" <?=$f[0]?>  class="form-control">1 : 形容詞
            <input type="checkbox" name="filter[]" value="2" <?=$f[1]?> class="form-control">2 : 形容動詞
            <input type="checkbox" name="filter[]" value="3" <?=$f[2]?> class="form-control">3 : 感動詞
            <input type="checkbox" name="filter[]" value="4" <?=$f[3]?> class="form-control">4 : 副詞
            <input type="checkbox" name="filter[]" value="5" <?=$f[4]?> class="form-control">5 : 連体詞
            <input type="checkbox" name="filter[]" value="6" <?=$f[5]?> class="form-control">6 : 接続詞
            <input type="checkbox" name="filter[]" value="7" <?=$f[6]?> class="form-control">7 : 接頭辞
            <input type="checkbox" name="filter[]" value="8" <?=$f[7]?> class="form-control">8 : 接尾辞
            <input type="checkbox" name="filter[]" value="9" <?=$f[8]?> class="form-control">9 : 名詞
            <input type="checkbox" name="filter[]" value="10"<?=$f[9]?>  class="form-control">10 : 動詞
            <input type="checkbox" name="filter[]" value="11"<?=$f[10]?>  class="form-control">11 : 助詞
            <input type="checkbox" name="filter[]" value="12"<?=$f[11]?>  class="form-control">12 : 助動詞
            <input type="checkbox" name="filter[]" value="13"<?=$f[12]?>  class="form-control">13 : 特殊(句読点、カッコ、記号など)
        </td>
    </tr>
    <tr>
        <td colspan="2"><input type="button" id="frmsubmit" value="登録する" class="form-control"></td>
    </tr>
</table>
</form>
<script>
    jQuery(function($){
        $("#frmsubmit").on("click",function(){
            var ajaxurl = '<?=admin_url( 'admin-ajax.php');?>';
            var data = $("#ajax-frm").serializeArray();
            data.push({name:"action",value:"ajax_event"});
            $.ajax({
               type:'POST',
               url:ajaxurl,
               data:data,
               success:function(obj){
                   console.log(obj);
                   if(obj.appid!==""){
                       alert("更新しました");
                   }
               }
            });
        });
    })
</script>

タグ

API, appid, array, Bootstrap, dirname, extend, foreach, implode, MAService, obj, parse, plugins, strlen, success, 助動詞, 品詞, 形容動詞, 接尾辞, 接頭辞, 連体詞,

(´ι _` )アッそうなんだそうなんだPHP fileメソッド

2015.11.09

Logging


PHPのfileメソッドっていうのが便利です。
ファイルを配列として引っこ抜いてくれる。こんな関数便利だなと
おそらく他の言語でも常識的にある関数なんだろうけど
自分はあまり知らなかったので便利だなと。
もうひとつ便利な関数は配列の中に空の値があったりすると
その配列を削除してくれるarray_filterとかいうものです。
これは便利・・・何故かってPHP、配列の中が空でも
値があるよって判断するです。そういう時に少し便利です。
emptyは使えないので・・・。もし配列が空でも必要な場合は
strlenとかでバイト数を数えるなどで対応するしか無いですね。
そういう事でメモがてらに残しときます。

$hoge = file("hoge.txt");
for($i=0;$i<sizeof($hoge);$i++){
echo hoge[$i]."<br>\n";
}
print($hoge);
$hoge2 = array_filter($hoge);
print($hoge2);

タグ

0, 2, array, br, echo, empty, file, filter, For, gt, hoge, lt, php, print, sizeof, strlen, txt, アッ, これ, バイド, ファイル, メソッド, メモ, もうひとつ, もの, , , , 便利, , 判断, 削除, 場合, 対応, 少し, 必要, , , 自分, 言語, 配列, 関数,