php中同一个字符串中的多个字符串替换

发布于 2024-07-17 01:37:23 字数 603 浏览 8 评论 0原文

我有以下字符串替换问题,我在这里很好地解决了

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅哥哥的热头脑 2024-07-24 01:37:23

我认为正则表达式会让这变得困难,但你应该能够仅使用 strpos() 来做到这一点substr()str_replace()

  • 使用strpos查找56和78在字符串中的位置。

  • < p>然后使用 substr 在这些点将字符串切割为子字符串。

  • 现在,将“quick”替换为正确的变量,具体取决于是否将 56 还是 78 发送到函数以及您正在处理哪个子字符串。

    现在,将

I think regular expressions will make this difficult but you should be able to do it using only strpos(), substr() and str_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.

故人爱我别走 2024-07-24 01:37:23

不要使用 preg_replace,而是使用 substr_replace 进行字符串替换和 strpos< /a> 根据您传递的参数查找字符串中的起点和终点。 您的模式是一个简单的字符串,因此不需要正则表达式,并且 substr_replace 将允许您指定字符串中的起点和终点以进行替换(这似乎就是您正在寻找的内容)。

编辑:

根据您的评论,听起来您必须进行大量检查。 我还没有测试过这个,所以它可能有一两个错误,但是尝试这样的函数:

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;
}

这​​个函数执行以下操作:

  1. 首先,检查“number”参数是否存在于字符串中($end_pos! == false)
  2. 检查您的模式在字符串开头和数字位置之间至少存在一次 (substr_count($input, $pattern, 0, $end_pos) )
  3. 使用 strrpos 函数获取子字符串中模式最后一次出现的位置
  4. 使用模式的起始位置和长度,使用 substr_replace 插入替换字符串

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).

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:

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:

  1. First, check that the "number" parameter even exists in the string ($end_pos !== false)
  2. 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))
  3. Use strrpos function to get the position of the last occurrence of the pattern within the substring
  4. Use the start position and the length of the pattern to insert your replacement string using substr_replace
晨光如昨 2024-07-24 01:37:23

尝试这个:

$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"

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"
灯角 2024-07-24 01:37:23

你的做法是错误的。 相反,根据您的函数输入,您应该使用正确的查找和替换值。 只需根据您的函数输入值创建查找和替换值的映射即可。 喜欢:

$map = array(
  56 => array('patterns' => array(), 'replacements' => array()),
  78 => array(...)
);

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(...)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文