2016-12-15 46 views
1

我想做一些相當簡單的事情,但我不知道PHP中的正確代碼。根據可變內容查找模式

我有一個變量$去哪些內容GO:XXXXX

我想詢問,如果一個變量的內容有模式「GO」和別的東西,呼應的東西,但如果沒有,呼應另一件事

我要聲明的if語句,如:

if (preg_match('/GO/', $go) { 
echo "something"; 
} 
else { 
echo "another thing"; 

但我不能讓它工作...

我想嵌入之間的這種說法我的腳本的這一部分:

$result = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where go_id like '%" . $go . "%'"); 

$items = mysqli_fetch_assoc($result); 

echo "<br/>"; 

echo "<b> The total number of genes according to GO code is:</b> " . $items['total']; 

mysqli_free_result($result); 

$result2 = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where db_object_name like '%" . $go . "%'"); 

$items2 = mysqli_fetch_assoc($result2); 

echo "<br/>"; 

echo "<b>The total number of genes according to GO association is:</b> " . $items2['total']; 

mysqli_free_result($result2); 

因爲它是現在,變量$旅途中能有像GO的值:XXXX或隨機句子,並使用此代碼,我得到兩個字符串,一個與價值0和另一個值根據與$ go內容匹配的總外觀。

我想要的是聲明一個if語句,以便它只打印一個字符串,即具有根據$ go內容匹配的數量的字符串,但不是兩個字符串。

任何幫助?

+0

的[startsWith()和的endsWith()PHP函數]可能的複製(http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php) – Yoshi

回答

3

使用strpos()

if (strpos($go, 'GO:') !== false) { 
    echo 'true'; 
} 
+0

非常感謝! –

1

試試這個

回聲(strpos($去, 'GO:'!))? 「另一件事」:「某事」;

一定將工作