0

我有一个字符串,其中包含不同数量的 Url。我想删除每个 Url 的“?category_id=x”部分。x 将是一个不断变化的数字。

var old_string = "some text <a href='/platforms/item/something?category_id=x'>some text</a> some more text <a href='/platforms/item/something?category_id=x'>some text</a>..." 

var new_string = "some text <a href='/platforms/item/something'>some text</a> some more text <a href='/platforms/item/something'>some text</a>...." 

我怎样才能做到这一点。正则表达式?

4

4 回答 4

0
<?php
 $old_string  = "some text <a href='/platforms/item/something?category_id=x'>some text</a>  some more text <a href='/platforms/item/something?category_id=x'>some text</a>...";
 preg_match( '/<a href=\'(.*?)\'>/', $old_string, $match );
 $url         = parse_url($match[1]) ;
 $new_string  = "some text <a href='".$url["host"].$url["path"];
 $new_string .= "'>some text</a> some more text <a href='/platforms/item/something?    category_id=x'>some text</a>...";
 echo htmlentities($new_string);
//output
// some text <a href='/platforms/item/something'>some text</a> some more text <a  href='/platforms/item/something?category_id=x'>some text</a>...
?>
于 2013-06-20T14:25:10.987 回答
0

有一个 parse_url 函数允许分离 url 字符串的组件。

http://php.net/manual/en/function.parse-url.php

于 2013-06-20T13:43:22.267 回答
0
<?php
$url = '//www.example.com/path?googleguy=googley';   
var_dump(parse_url($url));
?>

输出:-

array(3) {
  ["host"]=>
  string(15) "www.example.com"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(17) "googleguy=googley"
}

来源:- http://php.net/manual/en/function.parse-url.php

于 2013-06-20T13:45:11.900 回答
0

如果您已经从字符串中提取了 URL,则使用 parse_url 的答案是最好的。我假设你没有。

<?php
$old_string = "some text <a href='/platforms/item/something?category_id=x'>some text</a> some more text <a href='/platforms/item/something?category_id=x'>some text</a>...";

// if you know there won't be any other url parameters:
$new_string = preg_replace('!\?category_id=(x|\d+)!','', $old_string );

// otherwise, remove query string from all URLs:
$new_string = preg_replace('!(href=(?:\'|")[^\?\'"]+)\?[^\'"#]*!','\1', $old_string );

不过,您的代码看起来像 JavaScript。JS 中的第二个正则表达式是:

old_string.replace( /(href=(?:"|')[^\?'"]+)\?[^'"#]*/g, '$1' );
于 2013-06-20T13:55:05.407 回答