如何preg_replace链接以保留哈希值


How to preg_replace links so that the hash is kept

我有一个巨大的在线php动态文件集。它有链接,甚至一些无效的引号(与frontpage)

index2.php?page=xd
index2.php?page=xj asdfa
index2.php?page=xj%20aas
index2.php?page=xj#jumpword
index2.php?page=gj#jumpword with spaces that arenot%20
index2.php?page=afdsdj#jumpword%20with
index2.php?page=xj#jumpword with "quotes" iknow

$input_lines=preg_replace("/(index2.php?page'=.*)(#[a-zA-Z0-9_ ''"]*)('"'>)/U", "$0 --> $2", $input_lines);

我希望所有这些只是与# -部分,而不是有index2.php?页面= *部分。

整个晚上我都无法让这个工作。所以请帮忙。

在某些情况下,您可以使用parse_url从URL获取属性(例如:#后面的内容),如下所示:

$urls = array(
    'index2.php?page=xd',
    'index2.php?page=xj asdfa',
    'index2.php?page=xj%20aas',
    'index2.php?page=xj#jumpword',
    'index2.php?page=gj#jumpword with spaces that arenot%20',
    'index2.php?page=afdsdj#jumpword%20with',
    'index2.php?page=xj#jumpword with "quotes" iknow',
    );
foreach($urls as $url){
    echo 'For "' . $url . '": ';
    $parsed = parse_url($url);
    echo isset($parsed['fragment']) ? $parsed['fragment'] : 'DID NOT WORK';
    echo '<br>';
}
输出:

For "index2.php?page=xd": DID NOT WORK

为"index2.php吗?page=xj asdfa": DID NOT WORK

For "index2.php?page=xj%20aas": DID NOT WORK

For "index2.php?page=xj#jumpword": jumpword

为"index2.php吗?Page =gj#jumpword with空格不为%20";

For "index2.php?page=afdsdj#jumpword%20with": jumpword%20with

为"index2.php吗?Page =xj#jumpword with "quotes"我知道用"引号"连词我深知