PHP代表任意数字
PHP representing any number
我在 php 的帮助下制作了这个菜单,以便根据页面请求添加 .active
。
像这样:
<ul class="menu_lateral">
<li><a <?php if(($_REQUEST['p']=="estufas") || ($_REQUEST['p']=="estufas-1")){?> class="active" <?php } ?> href="index.php?p=estufas">Estufas</a></li>
<li><a <?php if($_REQUEST['p']=="recuperadores"){?> class="active" <?php } ?> href="index.php?p=recuperadores">Recuperadores</a></li>
<li><a <?php if($_REQUEST['p']=="salamandras"){?> class="active" <?php } ?> href="index.php?p=salamandras">Salamandras</a></li>
<li><a <?php if($_REQUEST['p']=="churrasqueiras"){?> class="active" <?php } ?> href="index.php?p=churrasqueiras">Churrasqueiras</a></li>
</ul>
正如您在第一项上看到的,还有另一个 ESTUFA 页面。因此,如果请求是 ESTUFAS,它将添加 .active
,如果请求是 ESTUFAS-1,也会添加它。
问题是,我想添加很多页面,estufas-1、2、3、4、5 等,为此我必须在我的 If 或者可能更改中添加很多 OR它到一个案例。但是这样好像工作量太大了,而且不会是动态的。
有没有办法让它变成任意数字,例如:
if(($_REQUEST['p']=="estufas") || ($_REQUEST['p']=="estufas-ANYNUMBER"))
因此如果请求是 estufas 或 estufas-1 或 estufas-2 或 estufas-3 等,它将添加 .active
那会让事情变得如此简单。
有什么办法吗?或者任何其他解决方案?
您可以随时使用 stripos 之类的东西来查找它是否存在
if(stripos($_REQUEST['p'], 'estufas') !== false) // We have estufas
如果条件为:
,您可以在内部使用此代码
preg_match('/^estufas(-\d)?$/', $_REQUEST['p'])
所以结果将是:
<ul class="menu_lateral">
<li><a <?php if(preg_match('/^estufas(-\d)?$/', $_REQUEST['p'])){?> class="active" <?php } ?> href="index.php?p=estufas">Estufas</a></li>
<li><a <?php if($_REQUEST['p']=="recuperadores"){?> class="active" <?php } ?> href="index.php?p=recuperadores">Recuperadores</a></li>
<li><a <?php if($_REQUEST['p']=="salamandras"){?> class="active" <?php } ?> href="index.php?p=salamandras">Salamandras</a></li>
<li><a <?php if($_REQUEST['p']=="churrasqueiras"){?> class="active" <?php } ?> href="index.php?p=churrasqueiras">Churrasqueiras</a></li>
</ul>
我在 php 的帮助下制作了这个菜单,以便根据页面请求添加 .active
。
像这样:
<ul class="menu_lateral">
<li><a <?php if(($_REQUEST['p']=="estufas") || ($_REQUEST['p']=="estufas-1")){?> class="active" <?php } ?> href="index.php?p=estufas">Estufas</a></li>
<li><a <?php if($_REQUEST['p']=="recuperadores"){?> class="active" <?php } ?> href="index.php?p=recuperadores">Recuperadores</a></li>
<li><a <?php if($_REQUEST['p']=="salamandras"){?> class="active" <?php } ?> href="index.php?p=salamandras">Salamandras</a></li>
<li><a <?php if($_REQUEST['p']=="churrasqueiras"){?> class="active" <?php } ?> href="index.php?p=churrasqueiras">Churrasqueiras</a></li>
</ul>
正如您在第一项上看到的,还有另一个 ESTUFA 页面。因此,如果请求是 ESTUFAS,它将添加 .active
,如果请求是 ESTUFAS-1,也会添加它。
问题是,我想添加很多页面,estufas-1、2、3、4、5 等,为此我必须在我的 If 或者可能更改中添加很多 OR它到一个案例。但是这样好像工作量太大了,而且不会是动态的。
有没有办法让它变成任意数字,例如:
if(($_REQUEST['p']=="estufas") || ($_REQUEST['p']=="estufas-ANYNUMBER"))
因此如果请求是 estufas 或 estufas-1 或 estufas-2 或 estufas-3 等,它将添加 .active
那会让事情变得如此简单。
有什么办法吗?或者任何其他解决方案?
您可以随时使用 stripos 之类的东西来查找它是否存在
if(stripos($_REQUEST['p'], 'estufas') !== false) // We have estufas
如果条件为:
,您可以在内部使用此代码preg_match('/^estufas(-\d)?$/', $_REQUEST['p'])
所以结果将是:
<ul class="menu_lateral">
<li><a <?php if(preg_match('/^estufas(-\d)?$/', $_REQUEST['p'])){?> class="active" <?php } ?> href="index.php?p=estufas">Estufas</a></li>
<li><a <?php if($_REQUEST['p']=="recuperadores"){?> class="active" <?php } ?> href="index.php?p=recuperadores">Recuperadores</a></li>
<li><a <?php if($_REQUEST['p']=="salamandras"){?> class="active" <?php } ?> href="index.php?p=salamandras">Salamandras</a></li>
<li><a <?php if($_REQUEST['p']=="churrasqueiras"){?> class="active" <?php } ?> href="index.php?p=churrasqueiras">Churrasqueiras</a></li>
</ul>