Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 若有,;最后找不到则全部更换_Php_Regex - Fatal编程技术网

Php 若有,;最后找不到则全部更换

Php 若有,;最后找不到则全部更换,php,regex,Php,Regex,我有一个这样的正则表达式 eregi_replace("s=.*&","",$section->urlToThreads); 它所做的是用'替换一切,它以's='开头,以'&' 我还想做的是,如果在字符串末尾的“s=”后面没有找到“&”,那么将从“s=”到字符串末尾的所有内容替换为“” 例如 test.php?s=12232dsd23423&t=41将变成test.php?t=41 及 test.php?t=41&s=12232dsd23423将成为test.php?t=41您可

我有一个这样的正则表达式

eregi_replace("s=.*&","",$section->urlToThreads);
它所做的是用
'
替换一切,它以
's='
开头,以
'&'

我还想做的是,如果在字符串末尾的“s=”后面没有找到“&”,那么将从“s=”到字符串末尾的所有内容替换为“”

例如

test.php?s=12232dsd23423&t=41将变成test.php?t=41


test.php?t=41&s=12232dsd23423将成为test.php?t=41

您可以将
&
设置为可选,并且只允许非
&
字符在两者之间匹配。此外,使用单词边界,以便只匹配
s=
(而不是
链接的子字符串=
):

但是您不应该再使用
ereg
。更新至
preg

$result = preg_replace('/\bs=[^&]*&?/', '', $section->urlToThreads);

您可以将
&
设置为可选,并且只允许非
&
字符之间进行匹配。此外,使用单词边界,以便只匹配
s=
(而不是
链接的子字符串=
):

但是您不应该再使用
ereg
。更新至
preg

$result = preg_replace('/\bs=[^&]*&?/', '', $section->urlToThreads);

s=.*(:&|$)
检查
&
行尾/字符串。

s=.*(:&|$)
检查
&
行尾/字符串。

如果是预替换,我会做:

preg_replace('@(\?)s(=[^&]*)?&?|&s(=[^&]*)?@', '\\1', $section->urlToThreads);
一些测试:

$tests = array(
    'test.php?s',
    'test.php?s=1',
    'test.php?as=1',
    'test.php?s&as=1',
    'test.php?s=1&as=1',
    'test.php?as=1&s',
    'test.php?as=1&s=1',
    'test.php?as=1&s&bs=1',
    'test.php?as=1&s=1&bs=1'
);
foreach($tests as $test){
    echo sprintf("%-22s -> %-22s\n", $test, preg_replace('@(\?)s(=[^&]*)?&?|&s(=[^&]*)?@', '\\1', $test));
}
输出:

test.php?s->test.php?
test.php?s=1->test.php?
test.php?as=1->test.php?as=1
test.php?s&as=1->test.php?as=1
test.php?s=1&as=1->test.php?as=1
test.php?as=1&s->test.php?as=1
test.php?as=1&s=1->test.php?as=1
test.php?as=1&s&bs=1->test.php?as=1&bs=1
test.php?as=1&s=1&bs=1->test.php?as=1&bs=1

如果是preg\u replace,我会:

preg_replace('@(\?)s(=[^&]*)?&?|&s(=[^&]*)?@', '\\1', $section->urlToThreads);
一些测试:

$tests = array(
    'test.php?s',
    'test.php?s=1',
    'test.php?as=1',
    'test.php?s&as=1',
    'test.php?s=1&as=1',
    'test.php?as=1&s',
    'test.php?as=1&s=1',
    'test.php?as=1&s&bs=1',
    'test.php?as=1&s=1&bs=1'
);
foreach($tests as $test){
    echo sprintf("%-22s -> %-22s\n", $test, preg_replace('@(\?)s(=[^&]*)?&?|&s(=[^&]*)?@', '\\1', $test));
}
输出:

test.php?s->test.php?
test.php?s=1->test.php?
test.php?as=1->test.php?as=1
test.php?s&as=1->test.php?as=1
test.php?s=1&as=1->test.php?as=1
test.php?as=1&s->test.php?as=1
test.php?as=1&s=1->test.php?as=1
test.php?as=1&s&bs=1->test.php?as=1&bs=1
test.php?as=1&s=1&bs=1->test.php?as=1&bs=1

解决方案-不带正则表达式

$str = $section->urlToThreads;
                        $url = '';
                        $url = $section->urlToThreads;
                        $pos = strpos( $str,'s=');
                        if ($pos)
                        {
                            $pos_ampersand =  strpos( $str,'&',$pos);

                            if ($pos_ampersand) //if ampersand is found after s=
                            {
                                $url = substr($str, 0, $pos) . substr($str, $pos_ampersand+1, strlen($str));
                            }
                            else // if no ampersand found after s=
                            {
                                $url = substr($str, 0, $pos-1);
                            }
                        }
                        $section->urlToThreads = $url;

解决方案-不带正则表达式

$str = $section->urlToThreads;
                        $url = '';
                        $url = $section->urlToThreads;
                        $pos = strpos( $str,'s=');
                        if ($pos)
                        {
                            $pos_ampersand =  strpos( $str,'&',$pos);

                            if ($pos_ampersand) //if ampersand is found after s=
                            {
                                $url = substr($str, 0, $pos) . substr($str, $pos_ampersand+1, strlen($str));
                            }
                            else // if no ampersand found after s=
                            {
                                $url = substr($str, 0, $pos-1);
                            }
                        }
                        $section->urlToThreads = $url;

在上面的第二个例子中,我还在s=前面添加了一个“?”,只是为了去掉s前面剩余的和。现在工作正常了。我希望这没问题。哎呀,你的解决方案不起作用,例如上面的例子1。在上面的第二个示例中,它在s=之前添加了一个“?”,只是为了删除s之前剩余的和。现在工作正常了。我希望这没问题。哎呀,你的解决方案不起作用,例如上面的例子1。它擦除t=41。在第一个示例中,它在“t=41”之前留下一个“&”,在第二个示例中,它在“t=41”之后留下一个“&”。是的,我已经发布了一个快速解决方法。前导的
?&
或尾随的
&
应该可以,对吧。在第一个示例中,它在't=41'之前留下了一个'&',在第二个示例中,它在't=41'之后留下了一个'&'。是的,我已经发布了一个快速解决方法。前导的
?&
或尾随的
&
应该可以,对吧。您能告诉我们您是如何解决的,以供将来的读者阅读吗?@M42-更新问题above@ImranOmarBukhsh:您应该将您的解决方案添加为答案(您可以回答自己的问题),然后将其从问题中删除。然后,您可以接受自己的答案,将问题标记为已解决。不要在问题的标题中写“已解决”。你能告诉我们你是如何解决的,以供未来读者阅读吗?@M42-更新问题above@ImranOmarBukhsh:您应该将您的解决方案添加为答案(您可以回答自己的问题),然后将其从问题中删除。然后,您可以接受自己的答案,将问题标记为已解决。不要在问题的标题中写“已解决”。我选择了只写一个脚本并得到了它。我选择了只写一个脚本并得到了它