开发者

How to get a string constructed from a filename to use a language define?

开发者 https://www.devze.com 2023-02-18 01:08 出处:网络
I have been searching high and low for the answer to this one: $file_type_array = array(); $file_type_array[] = array(\'id\' => $file, \'text\' => PULL_DOWN_DEFAULT);

I have been searching high and low for the answer to this one:

$file_type_array = array();
$file_type_array[] = array('id' => $file, 'text' => PULL_DOWN_DEFAULT);
  while ($file = readdir($resc)) {
  $ext = strrchr($file, ".");
  if ($ext == ".php") {
    $filename = str_replace(' ', "_", $file);
    $filename = str_replace('-', "_", $filename);
    $filename = str_replace($ext, "", $filename);

    $string_define_filename = 'TEXT_' . strtoupper($filename);

    $file_type_array[] = array('id' => $file, 'text' => $string_define_filename);
  }
}

So what I am trying to achieve is:

  1. Get filenames from dire开发者_运维百科ctory

  2. Remove any spaces or - from the names

  3. Uppercase them and prefix 'TEXT_'

and then my problem:

4) Output the result of define('TEXT_{filename}, 'This is the translated name of the filename'); rather than the uppercase text.

Can anyone help?


Use the constant function.


Your code can be rewrited as follow:

$file_type_array[] = array('id' => $file, 'text' => PULL_DOWN_DEFAULT);
 while ($file = readdir($resc)) {
  $f = pathinfo($file);
  if ($f['extension'] == "php") {
    $filename = preg_replace('/[^\w\d]+/i','_',$f['filename']);
    $string_define_filename = 'TEXT_' . strtoupper($filename);
    define($string_define_filename,$file); //optional
    $file_type_array[] = array('id' => $filename, 'text' => constant($string_define_filename));
  }  
}
foreach( $file_type_array as $def ) {
       echo $def['text'] . "<br />";
}


Using constant as suggested this worked for me:

$file_type_array = array();
$file_type_array[] = array('id' => $file, 'text' => PULL_DOWN_DEFAULT);
while ($file = readdir($resc)) {
  $ext = strrchr($file, ".");
  if ($ext == ".php") {
    $filename = str_replace(' ', "_", $file);
    $filename = str_replace('-', "_", $filename);
    $filename = str_replace($ext, "", $filename);

  if (constant('TEXT_' . strtoupper($filename)) != null) {
    $filename_string = constant('TEXT_' . strtoupper($filename));
  } else {
    $filename_string = 'Missing language define for TEXT_' . strtoupper($filename);
}

  $file_type_array[] = array('id' => $file, 'text' => $filename_string);
  } 
}

(Added a conditional check in case filename not defined in the language file)

Thank you to everyone who posted for their help!

0

精彩评论

暂无评论...
验证码 换一张
取 消