str_replace 数组中的字符串,编号自动增加

发布于 2024-08-17 19:30:26 字数 342 浏览 2 评论 0原文

我们可以采取哪些措施来删除 page=2& From:

"page=2&param1=value1&param2=value2" or
"param1=value1&page=2&param2=value2".

成为:

"param1=value1&param2=value2" or
"param1=value1&param2=value2".

在 page=2 的情况下,2 是任何自然数。即(0 到 32000)。

问候,

what we can do to remove page=2&
From:

"page=2¶m1=value1¶m2=value2" or
"param1=value1&page=2¶m2=value2".

become:

"param1=value1¶m2=value2" or
"param1=value1¶m2=value2".

in case of page=2, 2 is any natural no. i.e. (0 to 32000).

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

内心旳酸楚 2024-08-24 19:30:26

您可以使用 parse_str 将字符串解析为数组,从数组中取消设置页面,然后使用 http_build_query重建结果。

代码示例:

$str = 'page=2¶m1=value1¶m2=value2';
$arr = array();
parse_str($str, $arr);
unset($arr['page']);
echo http_build_query($arr);

You can use parse_str to parse the string to an array, unset page from the array, and then use http_build_query to reconstruct the result.

Code example:

$str = 'page=2¶m1=value1¶m2=value2';
$arr = array();
parse_str($str, $arr);
unset($arr['page']);
echo http_build_query($arr);
独自←快乐 2024-08-24 19:30:26
 $str = trim(preg_replace("/\bpage=\d+&?/", "", $str), "$");

正则表达式:

\b        # Match a "boundary" point (start of new word)
page=     # Match 'page='
\d+       # Match 1 or more digits
&?        # Match a '&' if it exists

外部的修剪将删除任何可能剩余的尾随 &

如果您想修剪任何内容,可以将 \d+ 替换为 [^&]+ 以匹配 & 以外的任何字符。

 $str = trim(preg_replace("/\bpage=[^&]+&?/", "", $str), "$");
 $str = trim(preg_replace("/\bpage=\d+&?/", "", $str), "$");

The regexp:

\b        # Match a "boundary" point (start of new word)
page=     # Match 'page='
\d+       # Match 1 or more digits
&?        # Match a '&' if it exists

The trim around the outside will remove any trailing & that might be leftover.

If you want to trim anything you can replace the \d+ with [^&]+ to match any characters other than &.

 $str = trim(preg_replace("/\bpage=[^&]+&?/", "", $str), "$");
空心空情空意 2024-08-24 19:30:26

使用爆炸?

$urlString = "page=2¶m1=value1¶m2=value2";
$arr = explode('&',$urlString);
print_r( $arr );
foreach( $arr as $var => $val ) {
    if( substr( $val, 0, 6 ) == 'param2' ) {
        unset( $arr[ $var ] );
    }
}
$urlString = implode('&',$arr);

using explode ?

$urlString = "page=2¶m1=value1¶m2=value2";
$arr = explode('&',$urlString);
print_r( $arr );
foreach( $arr as $var => $val ) {
    if( substr( $val, 0, 6 ) == 'param2' ) {
        unset( $arr[ $var ] );
    }
}
$urlString = implode('&',$arr);
染墨丶若流云 2024-08-24 19:30:26

如果我理解正确的话,这将允许您删除任意页面(只要您知道页面数量):

function replacePage($querystring, $page = 1) {
    return str_replace('page='.$page.'&', '', $querystring);
} 

顺便说一句,如果可以避免的话,使用正则表达式并不是最佳的编码方式。它们速度很慢,应该用其他方法替换,例如 str_replace 或尽可能标记化。就个人而言,如果我知道必须从查询字符串中删除哪个页面,我会使用我的方法,因为它的运行速度比 preg_replace 快得多。

If I understood you correctly, this will let you remove an arbitrary page (as long as you know the number):

function replacePage($querystring, $page = 1) {
    return str_replace('page='.$page.'&', '', $querystring);
} 

As a side note, using regular expressions is not the optimum way to code if they can be avoided. They are slow and should be replaced with other methods such as str_replace or tokenizing where possible. Personally I'd use my method If I knew what page I had to remove from the query string as it will run much faster than a preg_replace.

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