2012-04-13 57 views
2

确定以明确病举一个例子有没有PHP函数来查找中间字符串?

$email = '[email protected]'; 
$domain = strstr($email, '@'); 
echo $domain; // prints @example.com 

$user = strstr($email, '@', true); // As of PHP 5.3.0 
echo $user; // prints name 

,因为它说,它之前打印什么“@”用真实的,空白打印接下来@

即时寻找一个功能打印@本身给它2串并抢他们之间有什么

这样

$string= 'someXthing'; 
    $tograb = phpfunction("some","thing"); 
    echo $tograb; // should be printing X 

^这并不工作,我只是写我吨至讲解

+0

我真的很想看到一个你可以使用它的情况。此外,只需使用正则表达式 – Ibu 2012-04-13 17:38:39

+0

你需要的是一个正则表达式 – 2012-04-13 17:39:41

+0

检查这个线程http://stackoverflow.com/questions/1445506/get-content-between-two-strings-php – 2012-04-13 17:40:46

回答

1

我不知道这是否原生功能,但你可以使用正则表达式

$string= 'someXthing'; 
preg_match("/some(.*)thing/",$string,$matches); 
var_dump($matches[1]); 

了解更多关于preg_match

+0

that kinda working,but how to make it to only print X without some thing – 2012-04-13 17:45:15

+0

@LiliAbedinpour变量$ matches是一个数组,因此您可以'echo $ matches [1]'查看匹配的字符串。 – Ibu 2012-04-13 17:49:41

+0

ohhh对不起,是现在得到它,感谢我很好,但我认为应该有一个功能,我想这将主要是在未来需要!不管怎么说,还是要谢谢你 – 2012-04-13 17:54:19

1

从互联网

function GetBetween($content,$start,$end){ 
    $r = explode($start, $content); 
    if (isset($r[1])){ 
     $r = explode($end, $r[1]); 
     return $r[0]; 
    } 
    return ''; 
} 

Original code

0

对于你说的例子,你可以让我们e strpos打印X这样:

$string = 'someXthing'; 

$start = strpos($string, "some") + strlen("some"); 
$end = strpos($string, "thing", $start); 
$tograb = substr($string, $start, $end - $start); 

echo $tograb; 

和X将被打印。