2013-04-10 82 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);