2011-11-11 51 views
0

我有以下字符串:得到一个复杂的字符串的子串在PHP

"@String RT @GetThisOne: Natio" 

如何从这个字符串得到“GetThisOne”?

+0

您确实需要扩展您的问题陈述。你想在RT后获得这个名字吗?会不止有一个?等等 – Aerik

+2

你需要更具体一些......文字的位置是否会从字符串变为字符串?你想要的可能是一个正则表达式... – Bryan

+0

你的意思是你想从字符串中提取“GetThisOne”吗? 你是否需要提取'@'和':'之间的字符串? 你到目前为止尝试过什么?什么不行? –

回答

2

您可以使用preg_match这样的:

<?php 

$string = "@String RT @GetThisOne: Natio"; 
preg_match('/@.*@([A-Za-z]*)/', $string, $matches); 
echo $matches[1]; // outputs GetThisOne 

这里的模式如下:在第二个@之后找到一个数字串。 Ideone example.

1

查找“@”位置,并在找到“@”后计算“:”的位置。

$at = strpos($string,'@'); 

substr($string,$at,strpos($string,':',$at)-$at); 
1

你总是可以尝试PHP的爆炸功能

$string = "@String RT @GetThisOne: Natio" 

$arr = explode('@', $string); 

if(is_array($arr) && count($arr)>0) 
{ 
    echo $arr[0]."\n"; 
    echo $arr[1]; 
} 

将回声出

字符串RT

GetThisOne:NATIO

+0

或者你可以缩短是 –