Php 多散列标签删除 函数getHashTagsFromString($str){ $matches=array(); $hashTag=array(); if(preg_match_all('/#([^\s]+)/',$str,$matches)){ 对于($i=0;$i
编辑:要匹配所有哈希标记,请使用:Php 多散列标签删除 函数getHashTagsFromString($str){ $matches=array(); $hashTag=array(); if(preg_match_all('/#([^\s]+)/',$str,$matches)){ 对于($i=0;$i,php,regex,Php,Regex,编辑:要匹配所有哈希标记,请使用: function getHashTagsFromString($str){ $matches = array(); $hashTag=array(); if (preg_match_all('/#([^\s]+)/', $str, $matches)) { for($i=0;$i<sizeof($match
function getHashTagsFromString($str){
$matches = array();
$hashTag=array();
if (preg_match_all('/#([^\s]+)/', $str, $matches)) {
for($i=0;$i<sizeof($matches[1]);$i++){
$hashtag[$i]=$matches[1][$i];
}
return $hashtag;
}
}
test string $str = "STR
this is a string
with a #tag and
another #hello #hello2 ##hello3 one
STR";
要删除,应使用preg\u replace
进行替换,而不是preg\u match\u all
preg_match_all('/#\S+/', $str, $match);
按如下方式更新正则表达式:
$repl = preg_replace('/#\S+/', '', $str);
/#+(\S+)/
说明:
-起始分隔符/
-将文字#+
字符匹配一次或多次#
-匹配(并捕获)任何非空格字符(简称(\S+)
)[^\S]
-结束分隔符/
$repl = preg_replace('/#\S+/', '', $str);
/#+(\S+)/
我正在查找哈希标记并将该字符串存储在数据库中。如果我删除哈希标记,如何存储字符串。但是您的标题说
哈希标记删除
我将编辑nowDownvoter:您能解释下一票吗?这可能会帮助我改进答案:)+1获得非常好的解释,并消除不必要的下一票