在提交的文本行中查找特定单词

时间:2014-02-06 00:43:12

标签: php

所以我有这样的代码:如果我输入带有特定单词的内容,它会显示出来,例如,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">';
 }

1 个答案:

答案 0 :(得分: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
}

请注意===的使用,在这种情况下必须使用它,否则它将无法正常使用。 (稍后您应该在php中查找=====之间的区别。)