简体   繁体   English

如果正则表达式用于文件夹和单个页面,我如何使用php

[英]how can I use php if regex for both folders and single pages

<?php if (preg_match('/\/(contact|news)\//', $_SERVER['REQUEST_URI']) === 1): ?>
   <a href="/">link</a>
<?php endif; ?>

Is there a way I can also specify a single page such as /index.html in a regex specifying folders in a php if ? 有没有办法我也可以在正则表达式中指定单个页面(例如/index.html ,以指定php if文件夹?

Written like this: 像这样写:

<?php 
if (preg_match('/\/(contact\/|news\/|index\.html)/', $_SERVER['REQUEST_URI'])): 
?>

You can define as many pages as you like (note the last / has been moved). 您可以定义任意多个页面(请注意最后一个/已被移动)。 Though, that could very quickly become unwieldy. 不过,这很快就会变得难以处理。

You may also wish to consider using preg_quote : 您可能还希望考虑使用preg_quote

<?php 
$startsWith = array(
    'contact/',
    'news/',
    'index.html'
);
foreach($startsWith as &$string) {
    $string = preg_quote($string);
}
if (preg_match('/\/(' . implode('|', $startsWith) . ')/', $_SERVER['REQUEST_URI'])): ?>

Which, especially if unfamiliar with regex syntax, would make managing things a little easier. 尤其是如果不熟悉正则表达式语法的话,这将使管理工作变得更加容易。

Try the below : 请尝试以下方法:

<?php if (preg_match('/\/(contact|news)\/index\.html/', $_SERVER['REQUEST_URI']) === 1): ?>
   <a href="/">link</a>
<?php endif; ?>

UPDATE UPDATE

Based on your comment below this is the code that should work : 根据您在下面的评论,这是应该起作用的代码:

<?php
// you can add .* after index\.html you want to match index.html with get variables
echo preg_match('/\/(index\.html.*|contact\/.*|news\/.*)/','/index.html');
// or just make it strict to match only index.html
echo preg_match('/\/(index\.html|contact\/.*|news\/.*)/','/index.html');
echo '<br>';
echo preg_match('/\/(index\.html|contact\/.*|news\/.*)/','/contact/blablabla');
echo '<br>';
echo preg_match('/\/(index\.html|contact\/.*|news\/.*)/','/news/blablabla');

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一apache安装中同时使用PHP 4和PHP 5? - How can I use both PHP 4 and 5 with the same apache install? 如何在带有表单的PHP页面中使用函数? - How Can I Use Functions in PHP Pages with Forms? 我如何访问Dropbox php中的文件夹 - how can i access the folders in Dropbox php 如何阻止用户使用PHP或mod重写查看我的网站上没有索引页但其他页面的文件夹? - How can I stop users from viewing folders that have no index page but other pages on my web site using PHP or mod rewrite? 如何在 PHP 的 While 循环中使用正则表达式? - How can I use Regex in a While Loop in PHP? 我如何使用正则表达式或DOM与PHP来获取一片HTML? - How can i use Regex or DOM with PHP to get a slice of HTML? 如何使用regex使用PHP从文件中删除一行? - How can I use regex to remove a line from a file with PHP? 我如何使用 mysqli 到 SELECT 以及根据 WHERE 条件删除和返回单行 - How can I use mysqli to SELECT and both DELETE and RETURN a single row based on WHERE condition 我如何使用单个类别表在 Laravel 的帖子、页面和广告部分中具有类别 - How can i use single category table for having category in Posts, Pages and Advertisement Section in Laravel 如何在相同或不同文件夹中包含 php 页面? - How to include php pages in same or different folders?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM