[英]How to combine two conditional statements in PHP
Okay i know this is a newb question, but how would i go about only performing IF 2 if IF 1 (text: test appears in data string.) I've tried combining the two but end up with all sorts of issues.好的,我知道这是一个新问题,但是如果 IF 1(文本:测试出现在数据字符串中),我将如何仅执行 IF 2。我尝试将两者结合起来,但最终遇到了各种问题。 So if test doesnt show up the loops skipped, if it does then the regex code i have in IF 2 will be ran.
因此,如果测试未显示跳过的循环,则将运行我在 IF 2 中的正则表达式代码。
$data = 'hello world "this is a test" last test';
// IF 1
if (stripos($data, 'test') !== false) {
}
// IF 2
if (preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
echo $data;
Either:任何一个:
if (stripos($data, 'test') !== false) {
if (preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
}
Or:或者:
if (stripos($data, 'test') !== false && preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
Both do the same thing.两者都做同样的事情。 The
&&
operator means "and". &&
运算符的意思是“和”。 The ||
||
operator means "or".运算符的意思是“或”。
Do you mean you want to nest one inside the other?你的意思是你想把一个嵌套在另一个里面?
if (stripos($data, 'test') !== false)
{
if (preg_match('/"[^"]*"/i', $data, $regs))
{
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
}
You could also change this to use &&
(which means "And"):您也可以将其更改为使用
&&
(这意味着“和”):
if (stripos($data, 'test') !== false && preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
Also, your code uses !==
.此外,您的代码使用
!==
。 Is that what you meant or did you mean !=
?这是你的意思还是你的意思
!=
? I believe they have different meanings - I know that !=
means "Not equal" but I'm not sure about !==
.我相信它们有不同的含义 - 我知道
!=
表示“不相等”,但我不确定!==
。
Simply nest your IF statements只需嵌套您的 IF 语句
if (stripos($data, 'test') !== false) {
if (preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
}
Or have I misunderstood your question?还是我误解了你的问题?
Saying "I've tried combining the two but end up with all sorts of issues" is quite vague.说“我已经尝试将两者结合起来,但最终遇到了各种各样的问题”是相当含糊的。 Combining how?
怎么结合? Nested like this?
这样嵌套? What issues?
什么问题?
if (stripos($data, 'test') !== false) {
if (preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.