php字符串常用函数

时间:2021-12-20 08:25:31

addslashes

print addslahes ('She said, "Great!"');
#output
#She said, \"Great!\

echo

echo "hello world"
#output
#hello world

explode/implode

#explode
$var = 'Learn PHP';
print_r (explode (' ',$var));
# Result:
# Array ( [0] => Learn [1] => PHP ) #implode
$array1 = array('one','two','three');
print (implode ('+',$array1));
# output
# one+two+three

htmlentities

print htmlentities('<br>An example');

# output
# &lt;br&gt;An example

htmlspecialchars

print htmlspecialchars('Is "this" true? 3 > 2', ENT_NOQUOTES);
# output
# Is "this" true? 3 &gt; 2

md5

print md5('1');

# Result:
# c4ca4238a0b923820dcc509a6f75849b

number_format

number_format (number, [number_of_decimals], [decimal_character], [thousand_separator_character])

print number_format(5432.105, 3, ',', '.');

# Result:
# 5.432,105

str_replace

str_replace ('match_pattern', 'replacement_pattern', 'text', [count])

print str_replace ('str','k','The string replacement function in PHP is str_replace');
#Result:
#The king replacement function in PHP is k_replace

strlen

print strlen('strlen function');
#output
#15

strstr

strstr ('string', 'match_pattern', [before_indicator])

print strstr("PHP strstr function", 'str'); #output 'strstr function'
print strstr("php strstr function", 'str',TRUE) #output 'PHP' [before_indicator]该参数自 php 5.3.0引入

substr

substr ('string', position_start, substr_length)

print substr('PHP substr is a substring function', 5, 3); #output 'ubs'

参考

http://www.1keydata.com/php-tutorial/string-functions.php

http://developer.51cto.com/art/200807/94916.htm