php常用字串總結

2021-07-29 14:45:09 字數 3894 閱讀 5528

strlen

功能是輸出字串的長度

<?php

$str = 'hello world';

$result = strlen($str);

echo

$result;//輸出字串長度 11

$str2 = '你好 世界';

$result2 = mb_strlen($str2,'utf8');//中文

echo

$result2;//輸出字串長度 5

?>

trim

去除字串首尾處的空白字元(或者其他字元)

<?php

$str2 = ' 你好 世界 ';

$result2 = mb_strlen(trim($str2),'utf8');

echo

$result2;//輸出字串長度 5

$str3 = ' 你好 世界 ';

$result3 = mb_strlen(ltrim($str3),'utf8');//去左空格

echo

$result3;//輸出字串長度 6

$str4 = ' 你好 世界 ';

$result4 = mb_strlen(rtrim($str4),'utf8');//去右空格

echo

$result4;//輸出字串長度 6

?>

substr

功能是返回字串的子串

<?php

//string substr ( string $string , int $start [, int $length ] )

$str = 'hello world';

$result = substr($str,1);

echo

$result;//輸出ello world

$str2 = 'hello world';

$result2 = substr($str2,2,2);

echo

$result2;//輸出ll

?>

substr_count

功能是計算字串出現的次數

<?php

//int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

$text = 'this is a test';

echo strlen($text); // 14

echo substr_count($text, 'is'); // 2

// 字串被簡化為 's is a test',因此輸出 1

echo substr_count($text, 'is', 3);

// 字串被簡化為 's is',因此輸出 1

echo substr_count($text, 'is', 3,4);

?>

substr_replace

功能是替換字串的子串

<?php

//mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

$text = 'this is a test';

echo substr_replace($text, 'mm',0,0);//mmthis is a test

echo substr_replace($text, 'bbaa',1,2); // tbbaas is a test

echo substr_replace($text, '',10,-2);//this is a st

?>

strtolower

strtoupper

ucfirst

lcfirst

功能是將字串轉化為大小寫

<?php

$text = 'this is a test';

$text2 = 'this is a test';

echo strtolower($text);//全部轉小寫

echo strtoupper($text);//全部轉大寫

echo ucfirst($text);//將字串的首字母轉換為大寫

echo lcfirst($text);//將字串的首字母轉換為小寫

echo ucwords($text2);//將字串中每個單詞的首字母轉換為大寫

?>

echo

die

功能是輸出字串

<?php

$text = 'this is a test';

echo

$text;//輸出

die($text);//退出前輸出

echo

$text;//,沒有執行

?>

nl2br

功能是 在字串所有新行之前插入 html 換行標記

<?php

$text2 = $text2 = "this is\natest\n".' not action \n';

echo nl2br($text2);//單引號裡未執行

?>

strip_tags

功能是 從字串中去除 html 和 php 標記

<?php

$text2 = "this is\natest\n";

echo nl2br($text2);

?>

join

功能是 將乙個一維陣列的值轉化為字串

<?php

$text2 = array(1,6,2,3);

echo join($text2,'-');//用橫線連線成字串

echo implode($text2,'-');//用橫線連線成字串,與join一樣

?>

md5

功能是 從字串中去除 html 和 php 標記

<?php

$text2 = "this is\natest\n";

echo md5($text2);//md5演算法加密

?>

strrev

功能是 反轉字串

<?php

$text2 = "this is a test";

echo strrev($text2);//輸出tset a si siht

?>

strpos

功能是 查詢字串首次出現的位置(不區分大小寫的是stripos )

<?php

$text2 = "this is a test";

echo strpos($text2,'is');//輸出2

echo stripos($text2,'is');//輸出2

?>

PHP常用字串函式總結

1查詢字元位置函式 strpos str sea rch,int 查 找sea rch在 str,search,int 查詢search在 str,se arch in t 查詢se arch 在str中的第一次位置從int開始 stripos str,search,int 函式返回字串在另乙個字串...

php 常用字串函式總結

1.格式化輸出 chop 是rtrim 的別名 ltrim trim nl2br 將 n轉換成 print,echo,printf sprintf echo 不是函式,print 是函式,有返回值,boolen,false,true printf 格式化輸出 函式,把文字格式化以後輸出,直接呼叫系統...

mysql常用字串 MYSQL常用字串函式寶典

mysql常用字串函式 想更進一步加強自己在查詢語句方面的能力,需要掌握常用函式。字串函式 1 concat s1,s2,sn 將s1,s2,sn串聯成乙個字串。exp root test 14 43 desc t1 field type null key default extra id int ...