PHP删除内容中的部分URL

时间:2013-06-20 13:41:52

标签: php

我有一个包含不同数量的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 个答案:

答案 0 :(得分:0)

parse_url函数允许分隔url字符串的组件。

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

答案 1 :(得分: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

答案 2 :(得分: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' );

答案 3 :(得分: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>...
?>