Home  >  Article  >  Backend Development  >  How to remove the content after a certain character in php

How to remove the content after a certain character in php

青灯夜游
青灯夜游Original
2022-04-22 20:04:403468browse

Method: 1. Use strpos() to find the position of the specified character, syntax "strpos($str,"specified character")"; 2. Use substr() to intercept from the beginning of the string to the specified character Position (because the position starts from zero, so you need to add 1), the syntax is "substr($str,0, position value 1)".

How to remove the content after a certain character in php

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

php remove a certain The content after a character

In PHP, you can use the strpos() and substr() functions to remove the content after a certain character

Implementation idea:

  • Use strpos function to find the position of the specified character, for example character d

  • Use substr function to intercept from the beginning of the string to character d position (because the position starts from zero, you must add 1)

Implementation example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "abcdefg";
$index = strpos($str,"d");
$res = substr($str,0,$index+1);
echo $res;
?>

How to remove the content after a certain character in php

Extended knowledge: You can also use the strpos() and substr() functions to remove the content before a certain character

Just use the substr function to intercept the string starting from the specified character position (no need at this time Added 1)

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "abcdefg";
$index = strpos($str,"d");
$res = substr($str,$index);
echo $res;
?>

How to remove the content after a certain character in php

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to remove the content after a certain character 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