简体   繁体   English

如何在变量中使用函数?

[英]How to use a function inside a variable?

What I'm trying to do here is make use of PHP's ability to create and write to files because I have like 350 pages to make all with the same line of code that differs by one number.我在这里尝试做的是利用 PHP 创建和写入文件的能力,因为我有 350 个页面来使用相同的代码行来制作所有页面,但只有一个数字不同。 Much rather do this through code than manually creating 350 pages!与其手动创建 350 页,不如通过代码来实现!

Each file will be (.php) and named after the title of the content it will have which has already been defined.每个文件都将是 (.php) 并以其已经定义的内容的标题命名。 However, as this will be the URL to reach the page, I need to format the title and use the formatted version as the filename.但是,由于这将是到达页面的 URL,我需要格式化标题并使用格式化版本作为文件名。

This is what I've got to start with:这是我必须开始的:

function seoUrl($string) {
  //Make lowercase
  $string = strtolower($string);
  //Clean up multiple dashes or whitespaces
  $string = preg_replace("/[\s-]+/", " ", $string);
  //Convert whitespaces and underscore to dash
  $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}

I found this function earlier on here and it worked perfectly for making the sitemap for all these pages.我在这里早些时候发现了这个功能,它非常适合为所有这些页面制作站点地图。 The URLs were just like I wanted.这些 URL 就像我想要的那样。 However, when I call the same function to do this for each title, I hit a snag.但是,当我为每个标题调用相同的函数来执行此操作时,我遇到了障碍。 I assume I have the code wrong somewhere so here's a piece of the file creation code:我假设我在某处有错误的代码,所以这里有一段文件创建代码:

//Content title to be formatted for the filename
$title1="Capitalized And Spaced Title";

//Formatting
$urlfile1="seoUrl ($title1)";

//Text to be written
$txt1="<?include 'tpl/pages/1.txt'?>";

//And the create/write file code
  $createfile1=fopen("$urlfile1.php", "w");
      fwrite($createfile1, $txt1);
      fclose($createfile1);

The code inserts the $txt values just fine, which is actually where I anticipated having a problem.代码插入 $txt 值就好了,这实际上是我预期会出现问题的地方。 But my files that are created include the function name and parenthesis, plus the title isn't formatted.但是我创建的文件包括函数名和括号,而且标题没有格式化。

I didn't have this problem on the sitemap page:我在站点地图页面上没有这个问题:

$url1="$domainurl/$pathurl/$title1.php";
$url2="$domainurl/$pathurl/$title2.php";
...

seoUrl($url1);
seoUrl($url2);
...

<?echo $url1?><br>
<?echo $url2?><br>
...

I've tried everything I can think of for the past couple hours now.在过去的几个小时里,我已经尝试了所有我能想到的方法。 What am I doing wrong here?我在这里做错了什么?

Try this i hope this might help you out.试试这个我希望这可以帮助你。 it will create file in proper format.它将以正确的格式创建文件。

function seoUrl($string) {
//Make lowercase
$string = strtolower($string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}

$title1 = "Capitalized And Spaced Title";
//Formatting
$urlfile1 = seoUrl($title1);
//Text to be written
$txt1 = "<?include 'tpl/pages/1.txt'?>";
//And the create/write file code
$fileName = "" . $urlfile1 . ".php";
$createfile1 = fopen($fileName, "w");
fwrite($createfile1, $txt1);
fclose($createfile1);

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

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