php中同一个字符串中的多个字符串替换
我有以下字符串替换问题,我在这里很好地解决了
PFB 示例字符串,
$string = 'The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/quick/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
我需要根据我发送的数字替换“快速”
,即如果我对函数的输入是 56
,则56
之前的quick
需要替换为bear
,如果我对函数的输入是78
,则之前的quick 78
需要替换为 black
有人可以帮我吗?
I have the following string replacement problem and I am in quite a fix here
PFB the sample string
$string = 'The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/quick/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
I need to replace 'quick' depending on the numbers i send
i.e if my input to a function is 56
, the quick
before 56
needs to be replaced with bear
and if my input to a function is 78
, the quick before 78
needs to be replaced with black
Can someone please help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为正则表达式会让这变得困难,但你应该能够仅使用
strpos()
来做到这一点、substr()
和str_replace()
。使用
strpos
查找56和78在字符串中的位置。substr
在这些点将字符串切割为子字符串。现在,将“quick”替换为正确的变量,具体取决于是否将 56 还是 78 发送到函数以及您正在处理哪个子字符串。
现在,将
I think regular expressions will make this difficult but you should be able to do it using only
strpos()
,substr()
andstr_replace()
.Use
strpos
to find the location in the string of 56 and 78.Then cut the string up into substrings at these points using
substr
.Now, replace 'quick' with the correct variable, depending on whether 56 or 78 was sent to the function and which substring you are dealing with.
不要使用
preg_replace
,而是使用substr_replace
进行字符串替换和 strpos< /a> 根据您传递的参数查找字符串中的起点和终点。 您的模式是一个简单的字符串,因此不需要正则表达式,并且 substr_replace 将允许您指定字符串中的起点和终点以进行替换(这似乎就是您正在寻找的内容)。编辑:
根据您的评论,听起来您必须进行大量检查。 我还没有测试过这个,所以它可能有一两个错误,但是尝试这样的函数:
这个函数执行以下操作:
$end_pos! == false
)substr_count($input, $pattern, 0, $end_pos)
)strrpos
函数获取子字符串中模式最后一次出现的位置substr_replace
插入替换字符串Instead of working with
preg_replace
, usesubstr_replace
to do your string replacement and strpos to find the start and end points within the string based on the parameters you pass. Your pattern is a simple string, so it doesn't require a regular expression, and substr_replace will allow you to specify a start and end point within the string to do replacements (which seems to be what you're looking for).EDIT:
Based on your comment, it sounds like you have to do a lot of checking. I haven't tested this, so it may have a bug or two, but try a function like this:
This function does the following:
$end_pos !== false
)substr_count($input, $pattern, 0, $end_pos)
)strrpos
function to get the position of the last occurrence of the pattern within the substringsubstr_replace
尝试这个:
Try this:
你的做法是错误的。 相反,根据您的函数输入,您应该使用正确的查找和替换值。 只需根据您的函数输入值创建查找和替换值的映射即可。 喜欢:
You are doing it the wrong way. Instead depending on your function input you should use the correct find and replace values. Just create a map of find and replace values depending on your function input value. Like: