PHP開発ノートシリーズ(2) - 文字列の使い方

WBOY
リリース: 2016-06-13 13:25:18
オリジナル
778 人が閲覧しました

PHP 開発ノート シリーズ (2) - 文字列の使用法

「PHP開発ノートシリーズ(1) - PDOの使い方」に続き、今日はPHP開発における文字列の処理についての「PHP開発ノートシリーズ(2) - 文字列の使い方」を開設し、「PHP開発ノート」としました。シリーズ(2) - 文字列の使い方』 「開発ノートシリーズ」の第2弾です。

文字列は、どの開発言語でも処理する必要があります。PHP では、一重引用符 (') または二重引用符 (") を使用して文字列を定義できます。一重引用符と二重引用符の違いは何ですか?二重引用符で囲まれた変数は変数値に置き換えられ、一重引用符で囲まれた内容はそのまま出力されます。日常のプログラム開発で遭遇する文字列処理のシナリオを以下にまとめます。 >
1. 文字列を配列としてアクセスします。 strlen)


2. テキストからすべての HTML タグを削除します (strip_tags)

file:str-lengh.php
url:http://localhost:88/str/str-lengh.php
<?php
    $word = 'Hello, Ryan!';
    echo "String($word)'s length: ".strlen($word)."<br/>";
    
    // for循环访问数组
    //for($i=0; $i<strlen($word); $i++){
    //    echo $word[$i],"<br/>";
    //}
    
    // while循环访问数组
    $i=0;
    while($i<strlen($word)){
         echo $word[$i],"<br/>";
         $i++
    }
?>
ログイン後にコピー

注: $text の値が

hello world! で、

が欠落している場合、

は、その後のフォーマット出力に影響を与えません。後続の出力は、h1 title
file:str-strip-tags.php
url:http://localhost:88/str/str-strip-tags.php
<?php
    // 字符串中的所有html标签都闭合
    $text = "<h1>hello world!</h1><h1>hello world!</h1><h1>hello world!</h1>";
    
    // 输出原始的字符串内容
    echo "Original Text:";
    echo $text."<br/>";
    
    // 去除所有html标签后进行输出
    echo "Destination Text(After strip_tags)"."<br/>";
    echo strip_tags($text)."<br/>";

    // 字符串中的html标签不闭合
    $text = "<h1>hello world!";
    
    // 去除所有html标签后进行输出
    echo "Original Text:";
    echo $text."<br/>";
    
    // 去除所有html标签后进行输出
    echo "Destination Text(After strip_tags)"."<br/>";
    echo strip_tags($text)."<br/>";
?>
ログイン後にコピー

3. HTML エンティティをエスケープします (rawurlencode)


4. テキストを強制的に折り返す (wordwrap)
file:str-entities.php
url:http://localhost:88/str/str-entities.php
<?php
    $text = "hello & world!";
    
    echo $text."<br/>";
    
    echo rawurlencode($text)."<br/>";
?>
ログイン後にコピー


wordwrap 関数は、指定された文字列の長さに従うことができ、長いテキストを折り返すことができます。 strpos 関数を使用します。この関数は、JAVA の String クラスのindexOf() メソッドの関数と同様に、別の文字列内で文字列が出現する最初の位置を返します:
文字列の置換では、str_replace 関数を使用します。この関数は、JAVA の String クラスの replace() メソッドと同様に、文字列の一部のテキストを置換します。
file:str-wordwrap.php
url:http://localhost:88/str/str-wordwrap.php
<?php
    $text = "This document covers the JavaTM 2 Platform Standard Edition 5.0 Development Kit (JDK 5.0). Its external version number is 5.0 and internal version number is 1.5.0. For information on a feature of the JDK, click on its component in the diagram below.";
    
    echo "Original text:"."<br/>";
    echo $text."<br/>";
    
    echo $text."<hr/>";
    
    echo "Destination text(after wrap):"."<br/>";
    echo wordwrap($text, 50, "<br/>")."<br/>";
?>
ログイン後にコピー


6. 文字列比較 (substr_compare)
文字列比較は、JAVA の String の比較メソッドと同様に、2 つの文字列のサイズを比較するために使用できます。戻り値が >0 の場合は、最初の文字列が 2 番目の文字列より大きいことを意味し、それ以外の場合、2 番目の文字列が最初の文字列より大きいことを意味します。
file:str-strpos.php
url:http://localhost:88/str/str-strpos.php
<?php
    $text = "hello world!";
    
    echo strpos($text, "e");  
?>
ログイン後にコピー


7. 文字列インターセプト (substr)
file:str-strreplace.php
url:http://localhost:88/str/str-strreplace.php
<?php
    $text = "This document covers the JavaTM 2 Platform Standard Edition 5.0 Development Kit (JDK 5.0). Its external version number is 5.0 and internal version number is 1.5.0. For information on a feature of the JDK, click on its component in the diagram below.";
   
    echo "Original text:"."<br/>";
    echo $text."<br/>";
    
    echo "<hr/>";
    
    echo "Destination text(replace):"."<br/>";
    echo str_replace(" ", "__", $text)."<br/>";    
?>
ログイン後にコピー

文字列インターセプトは、文字列内の指定された位置から指定された長さをインターセプトするために使用できます。文字列は部分文字列値を抽出するのに非常に便利です。 🎜>
親文字列に部分文字列が出現する回数をカウントするには、substr_count 関数を使用できます。
9. 文字列の分割と組み立て (explode、implode)
file:str-compare.php
url:http://localhost:88/file/str-compare.php
<?php
    $main_str = 'hello world';
    $str = 'hello world, Ryan!';
    echo substr_compare($main_str, $str, 0);
?>
ログイン後にコピー


文字列の分割JAVAのStringクラスのsplit()メソッドの機能と同様に、指定された区切り文字に従って配列に分割されます。文字列のアセンブリ中に、文字列配列内のデータが区切り文字に従って組み立てられ、新しい文字列が形成されます。
10. 文字列
file:str-sub.php
url:http://localhost:88/file/str-sub.php
<?php
    $str = 'hello world,today is sunday!';
    $start = strpos($str, ',');
    $newStr = substr($str, $start+1);

    echo 'Original String: '.$str.'<br/>';
    echo 'Destination String: '.$newStr.'<br/>';
?>
ログイン後にコピー


11. フォーマットされた出力 (printf)
フォーマットされた出力は、C 言語の printf 関数と同様の printf 関数または sprintf 関数を使用します。 :
file:str-count.php
url:http://localhost:88/file/str-count.php
<?php
    $str = 'abcdefgacef';
    
    echo substr_count($str, 'a');
?>
ログイン後にコピー


この記事のアドレス: http://ryan-d.iteye.com/blog/1543225

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!