In PHP, processing strings is a common requirement in daily development. As a powerful string search tool, the strrist function can help us quickly locate specific parts of a string. This article will introduce in detail how to use the strstr function to find and delete specific parts in a string, and explain practical application techniques based on examples.
The strrist function is used to find another string in a string, returning everything from the matching point to the end of the string. If not found, false is returned.
Function prototype:
stristr(string $haystack, string $needle, bool $before_needle = false): string|false
$haystack : The string being searched for.
$needle : The string to be looked for (case insensitive).
$before_needle : If set to true , the function returns the content from the start of the string to the matching point.
To give a simple example, suppose we have the following string:
<?php
$str = "Welcome to visitgitbox.net,There are a wealth of resources and tutorials here。";
$part = stristr($str, "gitbox.net");
echo $part;
Output:
gitbox.net,There are a wealth of resources and tutorials here。
Here strstr finds the location of gitbox.net and returns the content starting from that location to the end of the string.
strrist itself does not have the function of deletion, but it can help us locate specific parts. Combined with string splicing, we can implement the deletion operation.
Assuming that you delete gitbox.net and what appears in the string and what follows, you can do this:
Use strstr to get gitbox.net and its contents behind it.
Use str_replace to replace this part with an empty string.
Sample code:
<?php
$str = "Welcome to visitgitbox.net,There are a wealth of resources and tutorials here。";
$needle = "gitbox.net";
$found = stristr($str, $needle);
if ($found !== false) {
// Delete fromgitbox.netThe part that starts until the end of the string
$result = str_replace($found, "", $str);
} else {
$result = $str;
}
echo $result; // Output:Welcome to visit
If you want to delete only a specific string and keep the subsequent content, you can set the $before_needle parameter to true :
<?php
$str = "Welcome to visitgitbox.net,There are a wealth of resources and tutorials here。";
$needle = "gitbox.net";
// Get the content before a specific string
$before = stristr($str, $needle, true);
if ($before !== false) {
echo $before; // Output:Welcome to visit
} else {
echo $str;
}
This directly obtains the string part before gitbox.net , eliminating subsequent replacements.
strristr is a useful tool for finding strings without being case sensitive.
Set the third parameter to true to quickly get the part before the target string.
Combined with str_replace or string splicing, the function of deleting the target string and its subsequent content can be realized.
If you need to delete the specific content before and after the target string, it is recommended to combine strpos and substr to operate accurately.
By flexibly using strrist , you can easily and quickly handle string search and deletion requirements, greatly improving the efficiency of string operation.