php轮换字符串中首个字符串[不重复替换]

WBOY
Release: 2016-06-13 13:10:47
Original
910 people have browsed it

php替换字符串中首个字符串[不重复替换]

利用正则表达式的方法替换字符串,并且只替换首个字符串,其余重复的不替换,可以利用这方法给文章关键词替换上连接


/**
 +----------------------
 * 只替换字符串中关键字一次
 +----------------------
 * @param string $needle 需替换的字符串
 * @param string $replace 目标字符串
 * @param string $haystack 原始字符串
 +----------------------
 * @return string
 +----------------------
 */
function str_replace_once($needle, $replace, $haystack) {
	$pos = strpos($haystack, $needle);
	if ($pos === false) {
		return $haystack;
	}
	return substr_replace($haystack, $replace, $pos, strlen($needle));
}
Copy after login

?

海口? http://www.souhaikou.com

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