Commit 2d3435c1 authored by 王源's avatar 王源 🎧

添加用替换字符串替换第一个出现的搜索字符串全局方法

parent 8d2272dc
...@@ -759,6 +759,7 @@ if (!function_exists('mb_trim')) { ...@@ -759,6 +759,7 @@ if (!function_exists('mb_trim')) {
* 去除两边带全角空格的字符串 * 去除两边带全角空格的字符串
* @param $str * @param $str
* @return string * @return string
* @author zero
*/ */
function mb_trim($str) function mb_trim($str)
{ {
...@@ -767,3 +768,22 @@ if (!function_exists('mb_trim')) { ...@@ -767,3 +768,22 @@ if (!function_exists('mb_trim')) {
return trim($str); return trim($str);
} }
} }
if (!function_exists('str_replace_once')) {
/**
* 用替换字符串替换第一个出现的搜索字符串
* @param mixed $search 搜索字符串
* @param mixed $replace 替换字符串
* @param mixed $subject 被替换字符串
* @return string
* @author zero
*/
function str_replace_first($search, $replace, $subject)
{
if (($position = strpos($subject, $search)) !== false) {
$replaceLen = strlen($search);
$subject = substr_replace($subject, $replace, $position, $replaceLen);
}
return $subject;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment