10.1、字符串格式化

 
 PHP Code By http://t.qq.com/tony-src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php 
    
    /**
     * 清理空格
     */

    $str = 
'          PHP            ';
    
echo ltrim($str);   // 去左边空格
    echo rtrim($str);   // 去右边空格,chop() 为rtrim()别名
    echo trim($str);    // 清理左右空格
    
    
/**
     * 将换行符换成<br>
     */

    $str = 
"This is a Teacher!\n This is Student";
    
echo nl2br($str);
    
    
/**
     * 所有字符转换成 HTML
     */

    $str = 
'<strong>张三</strong>';
    
echo htmlentities($str);
    
    
/**
     * 转换特殊字符转换成 HTML
     */

    $str = 
'<strong>张三</strong>';
    
echo htmlspecialchars($str);
    
    
/**
     * 去除 HTML 标记
     */

    $str = 
'<strong>张三</strong>';
    
echo strip_tags($str);
    
    
/**
     * 转义字符串
     */

    $str = 
'This is a Teacher.His is a "tony",\n This is tony wang';
    
echo $string = addslashes($str);
    
    
/**
     * 反转义字符串
     */

    
echo $string;
    
echo stripslashes($string);
    
    
/**
     * 字符串转换
     */

    
echo strtoupper('tony');                // 转换成大写
    echo strtolower('TONY');                // 转换成小写
    echo ucfirst('tony');                   // 将第一个字母转换为大写
    echo ucwords('my name is tony!');       // 将每个单词第一个字母转换为大写
    
    $str = 
'tony';
    
echo str_pad($str, 10).'is good!';      // 字符串填充
    echo str_pad($str, 10,'#');             // 填充指定的内容
    echo str_pad($str, 10,STR_PAD_LEFT);    // 填充方向
    
?>




原文地址:https://www.cnblogs.com/tonycody/p/2808237.html