Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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-URL-从first中删除所有内容=&引用;散列,但不散列_Php_Regex - Fatal编程技术网

PHP-URL-从first中删除所有内容=&引用;散列,但不散列

PHP-URL-从first中删除所有内容=&引用;散列,但不散列,php,regex,Php,Regex,在PHP中,如何删除散列的第一个等号(包括第一个等号),但保留散列锚点。因此: $url=”https://somedomain.com/some/path/and/page.php?first-query=&code1=val1&code2=val2#要保留的哈希“ 将变成这样: $url=”https://somedomain.com/some/path/and/page.php?first-query#hash to keep“这个简单的正则表达式替换如何: <?php // de

在PHP中,如何删除散列的第一个等号(包括第一个等号),但保留散列锚点。因此:

$url=”https://somedomain.com/some/path/and/page.php?first-query=&code1=val1&code2=val2#要保留的哈希“

将变成这样:


$url=”https://somedomain.com/some/path/and/page.php?first-query#hash to keep“

这个简单的正则表达式替换如何:

<?php

// define start url
$url = "https://somedomain.com/some/path/and/page.php?first-query=&code1=val1&code2=val2#hash-to-keep" ;

// replace with empty string: everything 
// from/including first equal sign that's not a hash
$newUrl = preg_replace("/=[^#]+/", "", $url) ;

die( sprintf("new URL is : %s", $newUrl) )  ;

?>

尝试使用
strtok()
?尝试
=.*(?=\\\\35;)
如果值为空,使用
*
而不是
+
怎么样?@B适用于此特定URL的Aerts,但在我的真实URL中,有许多长的社交跟踪代码,它也会删除哈希锚。@B我意识到哈希丢失实际上并不重要,因为在重定向时,我的JavaScript无论如何都会将其添加回去。谢谢您!