回答
你也可以用substr來解決。
在這裏,你去與substr
:
$a = '?_otherstuff_#id=756';
$var = substr($a, 0, strpos($a, "#"));
這種方法是多餘的,有不必要的開銷。這也不是'substr()'的功能來做這些動作。我很難過:P – 2013-04-10 13:11:47
$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
T +他的問題是我該如何刪除散列,而不僅僅是$ id = 756 – csaron92 2013-04-10 11:00:57
你的問題太模糊了,或者你沒有把握這個想法;)。也許這樣? '$ my_string = preg_replace('/ _ otherstuff _#/','_otherstuff_',$ my_string)' – 2013-04-10 11:03:06
如果你想刪除哈希按您的問題標題
$str = "?_otherstuff_#id=756'";
$newstr = str_replace("#","", $str);
您可以使用parse_url,可以刪除片段後分裂的URL
scheme - e.g. http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hashmark #
可以構建URL通過http_build_url
http://us1.php.net/manual/en/function.http-build-url.php
- 1. 刪除基於哈希鍵名散裝Redis的哈希值
- 2. PHP哈希清除
- 3. 從URL中刪除哈希值
- 4. IE:哈希值在ReturnUrl中刪除
- 5. 從URL中刪除哈希值window.location.hash
- 6. 刪除重複值在哈希
- 7. 如何從哈希表中刪除值?
- 8. 使用哈希值
- 9. 刪除哈希URL(燈箱)
- 10. 刪除網址哈希
- 11. 使用PHP的CMAC-AES哈希值
- 12. 如果你有哈希散列如何刪除第一哈希值 - 紅寶石
- 13. PowerShell哈希表值從循環外的第二個哈希表中刪除
- 14. 刪除具有相同鍵值的哈希值
- 15. 用php檢索URL中的哈希值?
- 16. PHP MD5哈希
- 17. Perl哈希對同一哈希值內的哈希值的引用
- 18. 刪除哈希屬性通配符
- 19. 從哈希表中刪除項目(java)
- 20. 地址哈希已被刪除
- 21. 如何從Perl中刪除哈希鍵?
- 22. AngularJS與RequireJS路由刪除哈希
- 23. 刪除laravel中的哈希過程5.3
- 24. 刪除日誌行哈希字符
- 25. Java哈希集刪除重複
- 26. 在C++中刪除哈希表
- 27. jsTree從URL中刪除哈希
- 28. 從url中刪除哈希ID
- 29. 刪除哈希中的數組
- 30. 刪除嵌套哈希元素
從字符串中刪除? – Nick 2013-04-10 10:58:41
您需要在此提供更多信息 - 您嘗試使用該字符串以及如何獲取該字符串。 – 2013-04-10 10:59:15
當然,問題是我該如何刪除散列,而不僅僅是$ id = 756, 我需要沒有散列的字符串。 – csaron92 2013-04-10 11:00:33