2014-02-06 23 views
-2

所以我有這樣的代碼:如果我輸入某個特定單詞的話,它會顯示出來,例如if ($_POST['text']它會查找單詞smile並將它轉換爲一些其他文字$out_smile。這種方法效果很好,但是如果在文字之間添加文字(如"I love to smile"),它將無法識別"smile"它會將其識別爲"I love to smile"。我直覺地知道這個原因。有什麼方法可以添加一個字符串?在提交的文本行中尋找一個特定的單詞

if ($_POST['text'] == "Smile") { 
    $out_smile = 'My Code here <img src="URL">'; 
} 

我想做這樣的事情。是否有可能做這樣的事情?

if (Found in the entire $text if there is a word == "smile") { 
    $out_smile = 'My Code here <img src="URL">'; 
} 

OR

$Auto_detect_left = "Extra text in the left hand"; //I Dont know how i am gonna do it 
    $Auto_detect_right = "Extra text in the right hand"; //I Dont know how i am gonna do it 
    $Out_result = ".$Auto_detect_left.$text.$Auto_detect_right; 
if ($_POST['text'] == "$Out_result") { 
    $out_smile = 'My Code here <img src="URL">'; 
} 

回答

2

假設你是問,以驗證字符串包含不同的字符串中,你想要什麼可能是strpos

$haystack = 'arglebarglearglebargle smile!'; 
$needle = 'smile'; 
$pos = strpos($haystack, $needle); 

if ($pos === false) { 
    //$needle is not present in $haystack 
} else { 
    //$needle is in $haystack at position $pos 
} 

請注意使用===,它的使用是必須在這種情況下,否則不會總是正常工作。 (稍後,您應該查看=====之間的區別。)

相關問題