Php 如何在变量中使用函数?

Php 如何在变量中使用函数?,php,function,fwrite,Php,Function,Fwrite,我在这里试图做的是利用PHP的创建和写入文件的能力,因为我有350个页面,所有这些页面都使用相同的代码行,它们相差一个数字。与其手动创建350页,不如通过代码来完成 每个文件都将是(.php),并以其已定义内容的标题命名。但是,由于这将是到达页面的URL,我需要格式化标题并使用格式化版本作为文件名 这是我首先要做的: function seoUrl($string) { //Make lowercase $string = strtolower($string); //Clean u

我在这里试图做的是利用PHP的创建和写入文件的能力,因为我有350个页面,所有这些页面都使用相同的代码行,它们相差一个数字。与其手动创建350页,不如通过代码来完成

每个文件都将是(.php),并以其已定义内容的标题命名。但是,由于这将是到达页面的URL,我需要格式化标题并使用格式化版本作为文件名

这是我首先要做的:

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;
}
我在这里早些时候发现了这个功能,它非常适合为所有这些页面制作站点地图。URL和我想要的一样。然而,当我为每个标题调用相同的函数时,我遇到了一个障碍。我假设我在某个地方有错误的代码,所以这里有一段文件创建代码:

//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);
//要为文件名格式化的内容标题
$title1=“大写和空格标题”;
//格式化
$urlfile1=“seoUrl($title1)”;
//待编写的文本
$txt1=“”;
//以及创建/写入文件代码
$createfile1=fopen($urlfile1.php,“$w”);
fwrite($createfile1,$txt1);
fclose($createfile1);
代码很好地插入了$txt值,这正是我预期会出现问题的地方。但是我创建的文件包括函数名和括号,加上标题没有格式化

我在站点地图页面上没有此问题:

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

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

<?echo $url1?><br>
<?echo $url2?><br>
...
$url1=“$domainurl/$pathurl/$title1.php”;
$url2=“$domainurl/$pathurl/$title2.php”;
...
瑟尔(1美元);
瑟尔(2美元);
...


...

在过去的几个小时里,我已经尝试了我能想到的一切。我做错了什么?

试试这个,我希望这能帮到你。它将以正确的格式创建文件

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);
函数seoUrl($string){
//使小写
$string=strtolower($string);
//清除多个破折号或空白
$string=preg_replace(“/[\s-]+/”,“”,$string);
//将空格和下划线转换为破折号
$string=preg\u replace(“/[\s\u]/”、“-”、$string);
返回$string;
}
$title1=“大写和空格标题”;
//格式化
$urlfile1=seoUrl($title1);
//待编写的文本
$txt1=“”;
//以及创建/写入文件代码
$fileName=”“$urlfile1。“.php”;
$createfile1=fopen($fileName,“w”);
fwrite($createfile1,$txt1);
fclose($createfile1);

这个答案不能解释任何事情。这个答案不适合你,兄弟。。我刚才回答了关于文件名未格式化的问题注意如何“调用”
seoUrl
函数以及如何调用其他函数,例如
strtolower