I have the following string replacement problem and I am in quite a fix here
我有以下字符串替换问题,我在这里解决了很多问题
PFB the sample string
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);
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
即如果我对函数的输入是56,那么56之前的快速需要用bear替换,如果我对函数的输入是78,则78之前的快速需要用黑色替换
Can someone please help me with this?
有人可以帮我这个吗?
4 个解决方案
#1
I think regular expressions will make this difficult but you should be able to do it using only strpos()
, substr()
and str_replace()
.
我认为正则表达式会使这很难,但你应该只能使用strpos(),substr()和str_replace()。
-
Use
strpos
to find the location in the string of 56 and 78.使用strpos查找字符串56和78中的位置。
-
Then cut the string up into substrings at these points using
substr
.然后使用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.
现在,将'quick'替换为正确的变量,具体取决于是否将56或78发送到函数以及您正在处理哪个子字符串。
#2
Instead of working with preg_replace
, use substr_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).
不使用preg_replace,而是使用substr_replace进行字符串替换,并使用strpos根据您传递的参数查找字符串中的起点和终点。您的模式是一个简单的字符串,因此它不需要正则表达式,substr_replace将允许您指定字符串中的起点和终点以进行替换(这似乎是您正在寻找的)。
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:
根据您的评论,听起来您必须进行大量检查。我没有测试过这个,所以它可能有一两个bug,但尝试这样的函数:
function replace($number, $pattern, $replacement)
{
$input = "The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.";
$end_pos = strpos($input, $number);
$output = "";
if($end_pos !== false && substr_count($input, $pattern, 0, $end_pos))
{
$start_pos = strrpos(substr($input, 0, $end_pos), $pattern);
$output = substr_replace($input, $replacement, $start_pos, ($start_pos + strlen($pattern)));
}
return $output;
}
This function does the following:
此功能执行以下操作:
- First, check that the "number" parameter even exists in the string (
$end_pos !== false
) - Check that your pattern exists at least once in between teh beginning of the string and the position of the number (
substr_count($input, $pattern, 0, $end_pos)
) - Use
strrpos
function to get the position of the last occurrence of the pattern within the substring - Use the start position and the length of the pattern to insert your replacement string using
substr_replace
首先,检查字符串中是否存在“number”参数($ end_pos!== false)
检查您的模式在字符串的开头和数字的位置之间至少存在一次(substr_count($ input,$ pattern,0,$ end_pos))
使用strrpos函数获取子字符串中最后一次出现的模式的位置
使用模式的起始位置和长度来使用substr_replace插入替换字符串
#3
Try this:
$searchArray = array("word1", "sound2", "etc3");
$replaceArray = array("word one", "sound two", "etc three");
$intoString = "Here is word1, as well sound2 and etc3";
//now let's replace
print str_replace($searchArray, $replaceArray, $intoString);
//it should print "Here is word one, as well sound two and etc three"
#4
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:
你这样做是错误的。取决于您的函数输入,您应该使用正确的查找和替换值。只需根据函数输入值创建查找和替换值的映射。喜欢:
$map = array(
56 => array('patterns' => array(), 'replacements' => array()),
78 => array(...)
);
#1
I think regular expressions will make this difficult but you should be able to do it using only strpos()
, substr()
and str_replace()
.
我认为正则表达式会使这很难,但你应该只能使用strpos(),substr()和str_replace()。
-
Use
strpos
to find the location in the string of 56 and 78.使用strpos查找字符串56和78中的位置。
-
Then cut the string up into substrings at these points using
substr
.然后使用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.
现在,将'quick'替换为正确的变量,具体取决于是否将56或78发送到函数以及您正在处理哪个子字符串。
#2
Instead of working with preg_replace
, use substr_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).
不使用preg_replace,而是使用substr_replace进行字符串替换,并使用strpos根据您传递的参数查找字符串中的起点和终点。您的模式是一个简单的字符串,因此它不需要正则表达式,substr_replace将允许您指定字符串中的起点和终点以进行替换(这似乎是您正在寻找的)。
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:
根据您的评论,听起来您必须进行大量检查。我没有测试过这个,所以它可能有一两个bug,但尝试这样的函数:
function replace($number, $pattern, $replacement)
{
$input = "The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.";
$end_pos = strpos($input, $number);
$output = "";
if($end_pos !== false && substr_count($input, $pattern, 0, $end_pos))
{
$start_pos = strrpos(substr($input, 0, $end_pos), $pattern);
$output = substr_replace($input, $replacement, $start_pos, ($start_pos + strlen($pattern)));
}
return $output;
}
This function does the following:
此功能执行以下操作:
- First, check that the "number" parameter even exists in the string (
$end_pos !== false
) - Check that your pattern exists at least once in between teh beginning of the string and the position of the number (
substr_count($input, $pattern, 0, $end_pos)
) - Use
strrpos
function to get the position of the last occurrence of the pattern within the substring - Use the start position and the length of the pattern to insert your replacement string using
substr_replace
首先,检查字符串中是否存在“number”参数($ end_pos!== false)
检查您的模式在字符串的开头和数字的位置之间至少存在一次(substr_count($ input,$ pattern,0,$ end_pos))
使用strrpos函数获取子字符串中最后一次出现的模式的位置
使用模式的起始位置和长度来使用substr_replace插入替换字符串
#3
Try this:
$searchArray = array("word1", "sound2", "etc3");
$replaceArray = array("word one", "sound two", "etc three");
$intoString = "Here is word1, as well sound2 and etc3";
//now let's replace
print str_replace($searchArray, $replaceArray, $intoString);
//it should print "Here is word one, as well sound two and etc three"
#4
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:
你这样做是错误的。取决于您的函数输入,您应该使用正确的查找和替换值。只需根据函数输入值创建查找和替换值的映射。喜欢:
$map = array(
56 => array('patterns' => array(), 'replacements' => array()),
78 => array(...)
);