2012-04-13 52 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將被打印。