简体   繁体   English

如何preg_match一个目录然后返回该目录

[英]How to preg_match a directory then return that directory

I am trying to check for a directory that matches .core* and return it as a string. 我正在尝试检查与.core*匹配的目录,并将其作为字符串返回。

Example: If the directory name is .core-alpha , then return .core-alpha as a string. 示例:如果目录名称是.core-alpha ,则将.core-alpha作为字符串返回。 Or, if the found directory is .core-1-rc1 , return that as the string. 或者,如果找到的目录是.core-1-rc1.core-1-rc1其作为字符串返回。

This is what I am trying to accomplish: 这是我要完成的工作:

$str = '.core/';
$core = preg_match('.core.', $str);

define('ROOT_DIR', realpath(dirname(__FILE__)) .'/');

require_once(ROOT_DIR . $core . '/lib/app.php');
$app = new App();

That just returns an integer - ie: 那只是返回一个整数-即:

/home/ubuntu/workspace/1/lib/app.php

Any help would be appreciated - Thnx 任何帮助将不胜感激-Thnx

preg_match returns a 1 or 0, NOT the match. preg_match返回1或0,而不是匹配项。 So just test the return value: 因此,只需测试返回值:

$str = '.core/';
if (preg_match('/^\.core.*/', $str)) {
    $core = $str;
}

Check the first example: http://php.net/manual/en/function.preg-match.php 检查第一个示例: http : //php.net/manual/zh/function.preg-match.php

Hi you have a missing parameter. 您好,您缺少参数。 receiver of matched value should be passed by reference. 匹配值的接收者应通过引用传递。

can you try this 你可以试试这个吗

$str = '.core/';
   if (preg_match('/^\.core.*/', $str, $core)) {
      echo $core[0];
   }
   else
   {
      echo "No Match Found."
   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM