user742030
user742030

Reputation:

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 - i.e.:

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

Any help would be appreciated - Thnx

Upvotes: 1

Views: 60

Answers (2)

rjdown
rjdown

Reputation: 9227

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

Upvotes: 1

Nikko
Nikko

Reputation: 104

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

Upvotes: 0

Related Questions