Home > Article > Backend Development > How to display only some characters in php
Display method: 1. Use substr() to intercept the characters of the specified length from the string and return it. The syntax is "substr(string, starting position, number of characters)"; 2. Use substr_replace(), Syntax "substr_replace(string,'', starting position, number of characters to remove)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php only displays part Character methods
1. Use substr()
substr(string,start,length)
The function can be obtained from The specified position start
of the string intercepts characters of a certain length length
.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,hello,world'; echo substr($str, 7).'<br>'; echo substr($str, -5).'<br>'; echo substr($str, 7, 15).'<br>'; echo substr($str, 7, -5).'<br>'; ?>
2. Use the substr_replace()
substr_replace() function to replace the string Replace part of it with another string.
When the replacement value is set to a null character, the specified character can be deleted, leaving another part of the substring.
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello,world,hello,world'; echo substr_replace($str,"", 7).'<br>'; echo substr_replace($str,"",-5).'<br>'; echo substr_replace($str,"", 7, 15).'<br>'; echo substr_replace($str,"", 7, -5).'<br>'; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to display only some characters in php. For more information, please follow other related articles on the PHP Chinese website!