在PHP中比较路径字符串与通配符

时间:2012-07-21 15:58:21

标签: php string compare

如何比较此字符串:

commodity/search/oil/branch/index

用这个:

commodity/search/*/branch/index

虽然“油”被其他词取代,但它应该返回true。

2 个答案:

答案 0 :(得分:5)

$match = preg_match("/commodity\/search\/(.*)\/branch\/index/", "commodity/search/someotherword/branch/index");
如果找到匹配项,

$match将为true(或某些值评估为true,如1)。

注意:以上内容将匹配任何额外路径,例如commodity/search/some/other/word/branch/index

如果您只想匹配一个单词,而不是类似于路径结构的东西,那么您可以尝试这样的事情:

$match = preg_match("/commodity\/search\/[A-Za-z0-9_-]+\/branch\/index/", "commodity/search/some-OTHER_word/branch/index");

这只会匹配大写和小写的a-z字符,数字,连字符和下划线。根据需要进行调整。

答案 1 :(得分:0)

PHP有一个内置函数,名为fnmatch()

它提供了基本通配符的使用。

stream.flatMap(new FlatMapFunction<Object, Tuple2<Object, Object>>() {
    @Override
    public void flatMap(Object value, Collector<Tuple2<Object, Object>> out) throws Exception {
        out.collect(Tuple2.of(/*key1*/, value));
        out.collect(Tuple2.of(/*key2*/, value));
        out.collect(Tuple2.of(/*key3*/, value));
    }
}).partitionCustom(new Partitioner<Object>() {
    @Override
    public int partition(Object key, int numPartitions) {
        return /* desired partition */
    }
}, 0);

虽然不支持非POSIX兼容系统(参见PHP手册)。

相关问题