Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多散列标签删除 函数getHashTagsFromString($str){ $matches=array(); $hashTag=array(); if(preg_match_all('/#([^\s]+)/',$str,$matches)){ 对于($i=0;$i_Php_Regex - Fatal编程技术网

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获得非常好的解释,并消除不必要的下一票