我想从给定的字符串,使用php和正则表达式以@xxx开头的单词中列出特定的单词。
e.g:
沃尔沃卡车 - Rajnikanth - 史诗般的分裂壮举。 (现场测试6)@xyz Thalaivar Epic Split Rajanikanth Rajinikanth Rajani Auto Auto rikshaw以及@ xyz.abc http://another.net @ bac.def这是测试@pqr
我希望返回值像@xyz,@ xyz.abc,@ bac.def,@ pqr这句话来自上面的字符串。
我不知道正则表达。
答案 0 :(得分:3)
这样做
<?php
$input_lines="Volvo Trucks - Rajnikanth - The Epic Split feat. (Live Test 6) @xyz Thalaivar Epic Split Rajanikanth Rajinikanth Rajani Auto Auto rikshaw and also @xyz.abc http://another.net @bac.def this is tesging @pqr";
preg_match_all("/(@\w+[^\s+]+)/", $input_lines, $output_array);
print_r($output_array[1]);
<强>输出强>:
Array
(
[0] => @xyz
[1] => @xyz.abc
[2] => @bac.def
[3] => @pqr
)
答案 1 :(得分:0)
替代解决方案......虽然不是正则表达式..
<?php
$str='Volvo Trucks - Rajnikanth - The Epic Split feat. (Live Test 6) @xyz Thalaivar Epic Split Rajanikanth Rajinikanth Rajani Auto Auto rikshaw and also @xyz.abc http://another.net @bac.def this is tesging @pqr';
$newarr[]=array_map(function ($v){ return $v='@'.explode(' ',$v)[0];},explode('@',$str));unset($newarr[0][0]);
print_r($newarr);
<强> OUTPUT :
强>
Array
(
[0] => @xyz
[1] => @xyz.abc
[2] => @bac.def
[3] => @pqr
)