PHP開発ノートシリーズ(2) - 文字列の使い方
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
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/>"; ?>
file:str-entities.php url:http://localhost:88/str/str-entities.php <?php $text = "hello & world!"; echo $text."<br/>"; echo rawurlencode($text)."<br/>"; ?>
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/>"; ?>
file:str-strpos.php url:http://localhost:88/str/str-strpos.php <?php $text = "hello world!"; echo strpos($text, "e"); ?>
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/>"; ?>
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); ?>
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/>'; ?>
file:str-count.php url:http://localhost:88/file/str-count.php <?php $str = 'abcdefgacef'; echo substr_count($str, 'a'); ?>

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

CakePHP はオープンソースの MVC フレームワークです。これにより、アプリケーションの開発、展開、保守がはるかに簡単になります。 CakePHP には、最も一般的なタスクの過負荷を軽減するためのライブラリが多数あります。
