2016-12-15 49 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:'!))? “另一件事”:“某事”;

一定将工作