简体   繁体   中英

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.

Example: If the directory name is .core-alpha , then return .core-alpha as a string. Or, if the found directory is .core-1-rc1 , return that as the string.

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

preg_match returns a 1 or 0, NOT the match. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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