Home > Backend Development > PHP Problem > How to remove content after specified characters in php

How to remove content after specified characters in php

青灯夜游
Release: 2023-03-16 09:02:01
Original
2527 people have browsed it

Two methods: 1. Use the "substr_replace($str,"",strpos($str,"specified character") 1)" statement to replace the content after the specified character position with a null character; 2 , use the "substr($str,0,strpos($str,"specified character") 1)" statement.

How to remove content after specified characters in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php remove specified characters Two methods for the following content

Method 1: Using strpos() and substr_replace() functions

  • Use strpos () Find the position of the specified character. For example, the character d

  • ## uses substr_replace() to replace the content after the specified character with the empty character

substr_replace(() ) is used to replace characters starting from the specified position, and we need to start replacing from after the specified character, so the position value of starting replacement is "position 1 of the specified character".

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "abcdefg";
echo "原字符串:".$str."<br>";
$index = strpos($str,"d");
$res = substr_replace($str,"",$index+1);
echo "去除字符d后的内容:".$res;
?>
Copy after login

How to remove content after specified characters in php

Method 2: Use strpos() and substr() functions

  • Use strpos function to find Specify the position of the character, for example the character e

  • Use the substr function to intercept from the beginning of the string to the position of the specified character

  • <?php
    header(&#39;content-type:text/html;charset=utf-8&#39;);   
    $str = "abcdefg";
    echo "原字符串:".$str."<br>";
    $index = strpos($str,"e");
    $res = substr($str,0,$index+1);
    echo "去除字符e后的内容:".$res;
    ?>
    Copy after login

    How to remove content after specified characters in php

    Recommended learning: "

    PHP Video Tutorial"

    The above is the detailed content of How to remove content after specified characters in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template