1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】よく使う関数とパターン(随時更新)

Last updated at Posted at 2019-08-09

:raised_hands:  随時更新予定  :raised_hands:

概要

・個人のメモ書きとして扱ってくださるとうれしです :ram:
・検索する手間を省くために当たり前のこと書いてます

文字列

:small_orange_diamond:特定文字を削除
str_replace ( mixed  指定する文字列, mixed 置換する文字列, mixed 対象の文字列, int $count※)
使用例

str_replace("%body%", "black", "<body text='%body%'>"); // <body text='black'> 
str_replace( array("o","l"), "", "Hello World of PHP"); // He Wrd f PHP
str_replace( array("o","l"), array("a","b"), "Hello World of PHP"); // Hebba Warbd af PHP

str_replace("ll", "", "good golly miss molly!", $count);
$count; // 2

※ 一致して置換が行われた箇所の個数がここに格納されます。


###### :small_orange_diamond:【文字列切り出し】 位置を指定 ``` substr(string 対象の文字列, int 開始位置※1, int 切り出す文字列の長さ)

日本語
mb_substr(string 対象の文字列, int 開始位置, int 切り出す文字列の長さ※2, string エンコーディング※3)

```ruby:使用例
substr('abcdef', 1); // bcdef
substr('abcdef', 0, 4); // abcd
substr('abcdef', -1, 1); // f

>> 日本語
substr('あいうえお', 1); // 文字化けしちゃう
mb_substr('あいうえお', 1, NULL, "UTF-8"); // いうえお 

※1 先頭は0、後ろからは-1
※2 NULLの場合は文字列の最後まで
※3 省略すると内部文字エンコーディングが使われるので文字化けの原因になる

:small_orange_diamond:【文字列切り出し】 文字を指定
strstr ( string 対象の文字列, mixed 指定する文字列, bool 開始位置※1)

>> 日本語
mb_strstr ( string 対象の文字列, string 指定する文字列, bool 開始位置※1, string エンコーディング)
使用例
strstr('abcdef', 'cd'); // cdef
strstr('abcdef', 'cd', true); // ab

>> 日本語
mb_strstr('あいうえお', 'う'); // うえお
mb_strstr('あいうえお', 'う', true); // あい

※1
trueの場合、指定した文字列より前の文字列を取得。
falseまたは、指定しない場合、指定したより後の文字列を取得。

:small_orange_diamond:【文字列切り出し】 文字を指定(正規表現)
使用例
preg_replace('/[^0-9]/', '', '456156円');// 456156

## 配列 ###### :small_orange_diamond:配列の重複を削除し、キーを振り直す ```ruby:使用例 $array = [1,2,3,2,1,4,5,3]; //重複削除 $unique= array_unique($array); //キーを振り直す $list = array_values($unique); ```
## ディレクトリ ###### :small_orange_diamond:ディレクトリ(フォルダ)の存在を確認 ```ruby:使用例 $path = "../hoge"; if(file_exists($path )){ echo "存在します"; } ```
###### :small_orange_diamond:ディレクトリ(フォルダ)の作成 ```ruby:使用例 $directory_path = "../hoge"; if(mkdir($directory_path, 0777)){ echo "作成に成功しました"; } ```
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?