回答
你也可以用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