本文介绍了没有空格的PHP hashtags字符串不会被分成hashtags的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用下面的代码:

  preg_match_all('/#([^ \s] +)/',$ str,$ matches); 

#test #test #test #example

用户插入带空格的井号标签。但是如果他们直接跟着对方呢?

 #test#test#test#example 

<$ p $
$ b $ p> preg_match_all('/#(\ w +)/',$ str,$ matches); b



$ b示例: code><?php
$ str ='#test#test2#123 qwe asd #rere#dada';
preg_match_all('/#(\ w +)/',$ str,$ matches);
var_export($ matches);

输出:

  array(
0 =>
array(
0 =>'#test',
1 =>'#test2',
2 =>'#123',
3 =>'#rere',
4 =>'#dada',
),
1 =>
数组(
0 =>'test',
1 =>'test2',
2 =>'123',
3 => 'rere',
4 =>'dada',
),

我认为学习RegEx可以帮助您解决这些问题。


I am trying to divide a string with hashtags into the single hashtag.

I am using this code:

preg_match_all('/#([^\s]+)/', $str, $matches);

#test #test #test #example

This one works fine if a user inserts the hashtags with spaces. But what about if they follow each other directly?

#test#test#test#example
解决方案

Try this:

preg_match_all('/#(\w+)/', $str, $matches);

Example:

<?php
$str = '#test #test2 #123 qwe asd #rere#dada';
preg_match_all('/#(\w+)/', $str, $matches);
var_export($matches);

Output:

array (
  0 => 
  array (
    0 => '#test',
    1 => '#test2',
    2 => '#123',
    3 => '#rere',
    4 => '#dada',
  ),
  1 => 
  array (
    0 => 'test',
    1 => 'test2',
    2 => '123',
    3 => 'rere',
    4 => 'dada',
  ),
)

I think learning RegEx will help you solve these problems.

这篇关于没有空格的PHP hashtags字符串不会被分成hashtags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:58