Home  >  Article  >  Backend Development  >  How to display only some characters in php

How to display only some characters in php

青灯夜游
青灯夜游Original
2022-04-26 13:45:292171browse

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)".

How to display only some characters in php

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 = &#39;hello,world,hello,world&#39;;
echo substr($str, 7).&#39;<br>&#39;;
echo substr($str, -5).&#39;<br>&#39;;
echo substr($str, 7, 15).&#39;<br>&#39;;
echo substr($str, 7, -5).&#39;<br>&#39;;
?>

How to display only some characters in php

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 = &#39;hello,world,hello,world&#39;;
echo substr_replace($str,"", 7).&#39;<br>&#39;;
echo substr_replace($str,"",-5).&#39;<br>&#39;;
echo substr_replace($str,"", 7, 15).&#39;<br>&#39;;
echo substr_replace($str,"", 7, -5).&#39;<br>&#39;;
?>

How to display only some characters in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn