2013-04-10 79 views
-4

如何使用php删除#id = 756?使用php删除哈希值

?_otherstuff_#id=756'

我试着substr()但它并没有工作正常。 感谢您的帮助。

+1

从字符串中删除? – Nick 2013-04-10 10:58:41

+1

您需要在此提供更多信息 - 您尝试使用该字符串以及如何获取该字符串。 – 2013-04-10 10:59:15

+0

当然,问题是我该如何删除散列,而不仅仅是$ id = 756, 我需要没有散列的字符串。 – csaron92 2013-04-10 11:00:33

回答

1

你也可以用substr来解决。

在这里,你去与substr

$a = '?_otherstuff_#id=756'; 
$var = substr($a, 0, strpos($a, "#")); 
+0

这种方法是多余的,有不必要的开销。这也不是'substr()'的功能来做这些动作。我很难过:P – 2013-04-10 13:11:47

2
$my_string = str_replace('#id=756', '', $my_string); 

或者使用正则表达式匹配的每个ID;

$my_string = preg_replace('/\#id=\d+/', '', $my_string); 

或者,只除去散,只有在那个地方(所以它不会意外匹配其他哈希漂浮在你周围的网址);

$my_string = preg_replace('/_otherstuff_#/', '_otherstuff_', $my_string) 

这里是一个与ω-矫枉过正,但表现出的可能性:

$counter = 0; 

$my_string = '?_otherstuff_#id=756&_otherstuff_#id=912'; 

$my_string = preg_replace_callback('/_otherstuff_#/', function($matches) { 
    $GLOBALS['counter']++; 

    echo ($matches[0]).' (repacement nr. '.$GLOBALS['counter'].')<br />'; 

    return '_otherstuff_'; 
}, $my_string); 

echo '<br />'.$my_string; 

输出

_otherstuff_# (repacement nr. 1) 
_otherstuff_# (repacement nr. 2) 

?_otherstuff_id=756&_otherstuff_id=912 
+0

T +他的问题是我该如何删除散列,而不仅仅是$ id = 756 – csaron92 2013-04-10 11:00:57

+0

你的问题太模糊了,或者你没有把握这个想法;)。也许这样? '$ my_string = preg_replace('/ _ otherstuff _#/','_otherstuff_',$ my_string)' – 2013-04-10 11:03:06

1

如果你想删除哈希按您的问题标题

$str = "?_otherstuff_#id=756'"; 

$newstr = str_replace("#","", $str);