2012-12-17 26 views
-4

删除我有一个字符串: /123.456.789.10:111213 我怎么能去掉'/'':111213',所以我仍然有123.456.789.10PHP - 从字符串

+3

什么是规则?描述要使用的规则。没有任何规则,最简单的答案是:'$ string ='123.456.789.10';'。简而言之,你有什么尝试? –

回答

0
$s = explode(":",$your_string); 
echo = substr($s[0], 1); 
0
echo substr($string, 1, strpos($string, ':')); 
0

不要使用正则表达式来实现这么简单的事情。字符串函数速度更快...

$old = '/123.456.789.10:111213'; 
$new = substr($old, strpos($old, '/') + 1, strpos($old, ':')); 
echo $new; 
0

有很多方法可以做到这一点,最简单的大概是这样的:

$result = split('[/:]', $your_string); 
$result = $result[1]; // gives "123.456.789.10" 

证明了它的工作原理:http://ideone.com/B6Kx6d

但它确实取决于初始字符串的许多变体,你要怎么养 - 另一种解决方案是低于(证明:http://ideone.com/Y6oW6F):

preg_match_all('</(.+)[:]>', $in, $matches); 
$result $matches[1][0]; // gives "123.456.789.10" 
0

如果你想使用常规表达匹配做到这一点:

input = "/123.456.789.10:111213"; 
echo preg_replace("/(\/)|(:111213)/", '', $input); 

虽然简单的字符串函数(以下回答)对于这种特定情况可能更快。