找出一段文本内的#hashtags,现在只能抓到由英文字母组成、以及以字母或数字结尾的tags(如 #中1)
请各位大大帮忙写出可以包含纯中文的hashtag(#论坛 #google #csdn论坛 #使用google)<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<h1>Get Hashtags</h1>
<?php
$subject = "捣乱的字符#tags #tagging #中文 #csdn论坛 #使用google google #143 #中1 #1中";
$pattern = '%((?:\A|(?<=\s))(?:#|@)(\w|(\p{L}\p{M}?))+\b)|(\[(#|@)[^\]]+\])%u';
preg_match_all($pattern, $subject, $matches);
$words = $matches[0];
print_r($words);
?>
</body>
</html>谢谢各位!
请各位大大帮忙写出可以包含纯中文的hashtag(#论坛 #google #csdn论坛 #使用google)<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<h1>Get Hashtags</h1>
<?php
$subject = "捣乱的字符#tags #tagging #中文 #csdn论坛 #使用google google #143 #中1 #1中";
$pattern = '%((?:\A|(?<=\s))(?:#|@)(\w|(\p{L}\p{M}?))+\b)|(\[(#|@)[^\]]+\])%u';
preg_match_all($pattern, $subject, $matches);
$words = $matches[0];
print_r($words);
?>
</body>
</html>谢谢各位!
想要的结果:Array ( [0] => #tagging [1] => #中文 [2] => #csdn论坛 [3] => #使用google [4] => #143 [5] => #中1 [6] => #1中 )
再次谢谢各位
preg_match_all('|(#[^\s]+\s+)|suU',$subject.' ',$match,PREG_PATTERN_ORDER);
print_r($match);
$subject = "捣乱的字符#tags #tagging #中文 #csdn论坛 #使用google google #143 #中1 #1中";
$pattern = '/#.+(?:[\s]{1}|$)/Um';
preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);
这段很有用,非常感谢你的帮助!不知高人能否再帮忙优化一下?
如果$subject是这样$subject = "#tags#tag #中文已经#能支持了";目前的代码返回的数组中会包含两个结果:#tags#tag #中文已经#能支持了
如果想把"#tags#tag"提取为两个结果"#tags、#tag"该如何做呢?
谢谢!