我现在已经把头包裹了好几天了,但似乎没有任何结果能达到预期效果。
示例:
$var = "Some Words - Other Words (More Words) Dash-Binded-Word";
期望的结果:
array(
[0] => Some Words
[1] => Other Words
[2] => More Words
[3] => Dash-Bound-Word
)
我能够使用preg_match_all完成所有工作,但接着是" Dash-Bound-Word"也被打破了。试图将它与周围的空间相匹配并不起作用,因为它会破坏除了破折号之外的所有单词。
我使用的preg_match_all语句(它也破坏了破折号的单词)是这样的:
preg_match_all('#\(.*?\)|\[.*?\]|[^?!\-|\(|\[]+#', $var, $array);
我当然不是preg_match,preg_split的专家,所以非常感谢这里的任何帮助。
答案 0 :(得分:4)
您可以使用简单的preg_match_all
:
\w+(?:[- ]\w+)*
请参阅demo
\w+
- 一个或多个字母数字或下划线(?:[- ]\w+)*
- 0个或更多个序列...
[- ]
- 连字符或空格(您可以将空格更改为\s
以匹配任何空格)\w+
- 一个或多个字母数字或下划线$re = '/\w+(?:[- ]\w+)*/';
$str = "Some Words - Other Words (More Words) Dash-Binded-Word";
preg_match_all($re, $str, $matches);
print_r($matches[0]);
结果:
Array
(
[0] => Some Words
[1] => Other Words
[2] => More Words
[3] => Dash-Binded-Word
)
答案 1 :(得分:2)
你可以拆分:
/\s*(?<!\w(?=.\w))[\-[\]()]\s*/
[\-[\]()]
尝试匹配(匹配任何这些字符)。您还可以将任何字符添加到该字符类中。(?<!\w)
来表示条件:“前面没有单词字符”。(?=.\w)
,用于检查:“如果满足第一个条件,则不应该跟随任何字符 - 用于拆分的字符和单词字符”。< / LI>
\s*
在开始,最后是修剪空格。$input_line = "Some Words - Other Words (More Words) Dash-Binded-Word";
$result = preg_split("/\s*(?<!\w(?=.\w))[\-[\]()]\s*/", $input_line);
var_dump($result);
array(4) {
[0]=>
string(10) "Some Words"
[1]=>
string(11) "Other Words"
[2]=>
string(10) "More Words"
[3]=>
string(16) "Dash-Binded-Word"
}
如另一条评论中所述,如果您还想捕捉括号:
$result = preg_split("/\s*(?:(?<!\w)-(?!\w)|(\(.*?\)|\[.*?]))\s*/", $input_line, -1, PREG_SPLIT_DELIM_CAPTURE);
答案 2 :(得分:0)
试试这个(str_replace和explode的组合)。这不是最佳的,但可能适用于这种情况:
$var = "Some Words - Other Words (More Words) Dash-Binded-Word";
$arr = Array(" - ", " (", ") ");
$var2 = str_replace($arr, "|", $var);
$final = explode('|', $var2);
var_dump($final);
输出:
array(4){[0] =&gt; string(10)“Some words”[1] =&gt; string(11)“其他 单词“[2] =&gt; string(10)”更多单词“[3] =&gt;字符串(16) “Dash-Binded-Word”}
答案 3 :(得分:0)
$var = "Some Words - Other Words (More Words) Dash-Binded-Word";
$var=preg_replace('/[^A-Za-z\-]/', ' ', $var);
$var=str_replace('-', ' ', $var); // Replaces all hyphens with spaces.
print_r (explode(" ",preg_replace('!\s+!', ' ', $var))); //replaces all multiple spaces with one and explode creates array split where there is space
输出: -
Array ( [0] => Some [1] => Words [2] => Other [3] => Words [4] => More [5] => Words [6] => Dash [7] => Binded [8] => Word )
答案 4 :(得分:0)
修改输入字符串以适合任何特定的爆炸技术将是间接的,并表明正在使用次优的爆炸技术。
事实是,您所需的逻辑可以归结为:“ 在长度为2或更大的每个非单词字符序列上爆炸”。 preg_split()
就是这种模式。
代码:(Demo)
$var = "Some Words - Other Words (More Words) Dash-Binded-Word";
var_export(preg_split('~\W{2,}~', $var));
输出:
array (
0 => 'Some Words',
1 => 'Other Words',
2 => 'More Words',
3 => 'Dash-Binded-Word',
)
没有比这更简单的了。