更改 php 中的特定文本

Changing specific text in php

$url = localhost/project/index.php?letter=0&position=0&bypass=1

如何把position=0改成position=1?

新的 $url 值将是:

$url = localhost/project/index.php?letter=0&position=1&bypass=1

您可以使用parse-str and parse-url approach with the help of http-build-query,

$url = "localhost/project/index.php?letter=0&position=0&bypass=1";
// fetching query paramters and save it to output variable
parse_str(parse_url($url,PHP_URL_QUERY),$output);
// changing position value
$output["position"] = 1;
// building back query string
$query = http_build_query($output);
// creating final string
echo parse_url($url,PHP_URL_PATH)."?".$query;

Demo
输出:-

localhost/project/index.php?letter=0&position=1&bypass=1

您必须使用 str_replace() 函数来替换字符串中的特定文本。

$url = str_replace('position=0','position=1',$url);