【実践】PHPでよく使う文字列関数の例まとめ【変換・置換・計算・傍受・暗号化】

黄舟
リリース: 2023-03-05 12:48:02
オリジナル
973 人が閲覧しました

この記事の例は、PHP で一般的に使用される文字列関数をまとめたものです。参考のために皆さんと共有してください。詳細は次のとおりです:

nl2br

機能: 改行を
に変更

<?php
$str = "cat isn&#39;t \n dog";
$result = nl2br($str);
echo $result;
/**结果
cat isn&#39;t
dog
*/
ログイン後にコピー

rtrim

機能: 右側の空白をクリアします

<?php
$str = "Hello world ";
echo strlen($str)."<br>";
$result = rtrim($str);
echo strlen($result);
/**结果
14
11
*/
ログイン後にコピー

strip_tags

機能: 文字列および php タグ内の HTML をクリアします

<?php
$str = "<font color = &#39;red&#39;>Hello world</font>";
$result = strip_tags($str);
echo $result;
/**结果
Hello world
*/
ログイン後にコピー

strto lower および strtoupper

機能: 大文字と小文字に変換します

<?php
$str = "Hello World!";
$result = strtolower($str);
echo $result."<br>";
$result = strtoupper($str);
echo $result;
/**结果
hello world!
HELLO WORLD!
*/
ログイン後にコピー

trim

機能: 先頭と末尾のスペースを削除します

<?php
$str = " Hello World! ";
$result = trim($str);
echo $str."<br>";
echo $result."<br>";
echo strlen($str)."<br>";
echo strlen($result);
/**结果
Hello World!
Hello World!
16
12
*/
ログイン後にコピー

str_ireplace

機能:

<?php
$str = "zhang san";
$result = str_ireplace("zhang","li",$str);
echo $str."<br>";
echo $result;
/**结果
zhang san
li san
*/
ログイン後にコピー
を置換します

str_repeat

関数: 文字列を置換します 複数回繰り返します

<?php
$str = "Hello jiqing!";
$result = str_repeat($str,4);
echo $str."<br>";
echo $result;
/**结果
Hello jiqing!
Hello jiqing!Hello jiqing!Hello jiqing!Hello jiqing!
*/
ログイン後にコピー

str_replace

関数: 大文字と小文字を区別する置換

<?php
$str = "hello jiqing!";
$result1 = str_ireplace("Hello","Hi",$str); //不区分大小写
$result2 = str_replace("Hello","Hi",$str); //区分大小写
echo $str."<br>";
echo $result1."<br>";
echo $result2."<br>";
/**结果
hello jiqing!
Hi jiqing!
hello jiqing!
*/
ログイン後にコピー

str_word_count

関数: 文字列内の単語数を返します

<?php
$str = "hello jiqing a!";
$result1 = str_word_count($str); //返回个数
$result2 = str_word_count($str,1); //返回数组
echo $str."<br>";
echo $result1."<br>";
print_r($result2);
/**结果
hello jiqing a!
3
Array ( [0] => hello [1] => jiqing [2] => a )
*/
ログイン後にコピー

strlen

関数: 文字列を返します文字列の長さ

<?php
$str = "hello jiqing a!";
$result = strlen($str);
echo $result;
/**结果
15
*/
ログイン後にコピー

substr_count

関数: ある文字列が別の文字列に含まれる数を数える

<?php
$str = "hello jiqing ,hello jim!";
$result = substr_count($str,"hello");
echo $result;
/**结果
2
*/
ログイン後にコピー

substr_replace

関数: 特定の位置から置換

<?php
$str = "hello jiqing ,hello jim!";
$result = substr_replace($str,"zhangsan",6);
echo $result."<br>";
$result = substr_replace($str,"zhangsan",6,6);//从某个位置替换,替换几个字符串
echo $result;
/**结果
hello zhangsan
hello zhangsan ,hello jim!
*/
ログイン後にコピー

substr

関数: 部分文字列を取得する

<?php
$str = "abcdef";
$result = substr($str,0,1); //从第0个开始,获取1个
echo $result."<br>";
$result = substr($str,0,-1);//从第0个开始,获取到除了最后一个的字符串
echo $result."<br>";
$result = substr($str,2,-1);//从第2个开始,获取到除了最后一个的字符串
echo $result."<br>";
$result = substr($str,-3,-1);//从第-3个开始,获取到除了最后一个的字符串
echo $result."<br>";
$result = substr($str,-3,1);//从第-3个开始,获取到除了最后一个的字符串
echo $result."<br>";
/**结果
a
abcde
cde
de
d
*/
ログイン後にコピー

implode

関数: Replace 配列を文字列に変換する

<?php
$array = array("2016","6","3");
$date = implode("/",$array);
echo $date;
/**结果
2016/6/3
*/
ログイン後にコピー

md5

関数: md5で文字列を暗号化する

<?php
$str = "Hello world";
$result = md5($str);
echo $result;
/**结果
3e25960a79dbc69b674cd4ec67a72c62
*/
ログイン後にコピー

以上がPHPの一般的な文字列関数の例【変換、置換、計算、傍受、暗号化】のまとめです(実践編) ) など さらに関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) に注目してください。


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