如何在 PHP 中组合两个条件语句 [英] How to combine two conditional statements in PHP

查看:37
本文介绍了如何在 PHP 中组合两个条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我知道这是一个新问题,但是如果 IF 1(文本:测试出现在数据字符串中),我将如何仅执行 IF 2.我尝试将两者结合起来,但最终遇到了各种问题.因此,如果测试未显示跳过的循环,如果显示,则将运行我在 IF 2 中的正则表达式代码.

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. So if test doesnt show up the loops skipped, if it does then the regex code i have in IF 2 will be ran.

$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;

推荐答案

要么:

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);
    }
}

或者:

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".

这篇关于如何在 PHP 中组合两个条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆