1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字列の先頭のクォーテーションをチェックして除外したいときの処理方法

Posted at

PHPで文字列の先頭にクォーテーションが付いてるかどうかをチェックして、ついていた場合にはそのクォーテーションを取り除く処理について忘備録としてまとめたものになります。

以下、サンプルコードになります。

<?php

    $string = "'example";

    // 先頭にクォーテーションがついているかどうかを確認
    if (substr($string, 0, 1) === "'") {
        // クォーテーションを取り除く
        $string = substr($string, 1);
    }
?>

$stringの中身が連想配列だった場合のサンプルコードも載せておきます。

<?php

    $array = [
        "key1" => "example1",
        "key2" => "'example2",
        "key3" => "'example3",
    ];

    foreach ($array as $key => $value) {
        if (is_string($value) && substr($value, 0, 1) === "'") {
            $array[$key] = substr($value, 1);
        } 
    }
?>

エクセルなどでアップロードしたときに、先頭にクォーテーションが含まれたときに役に立ちます。
参考になれば嬉しいです。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?