我正在使用此代码:
preg_match("/^.*\[Ticket ID\: \#(\d*)\].*$/", $subject, $output_array);
$ticketnumber = $output_array[1];
回显字符串中的数字,如:
[Ticket ID: #1234]
$ subject变量中的并将其放入$ticketnumber
变量
我如何检查主题变量中是否存在[Ticket ID: #1234]
?
答案 0 :(得分:0)
尝试strpos()
:
示例:
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
在您的方案中,您需要尝试这样:
$findme='[Ticket ID: #1234]';
$pos = strpos($subject, $findme);
if($pos){
//it contains
}
else {
//not containing
}
答案 1 :(得分:0)
我假设您正在尝试确定提供的主题中是否存在ticketnumber。
您可以尝试这样的事情:
<?php
// input
$subject = "[Ticket ID: #1234]";
// parse
preg_match("/^.*\[Ticket ID\: \#(\d*)\].*$/", $subject, $output_array);
$ticketnumber = isset($output_array[1]) ? intval($output_array[1]) : null;
// validate
if ($ticketnumber)
{
// A ticket number was found in subject
}
else
{
// not found
}
?>