写入文件与定义常量有错误不放置他的值


write file with define constant having error to not put his value

下面是我的代码,我需要在带有定义路径的文件中写入URL值。 但它输出为 URL,而不是替换为他的定义值,$string数据库中的变量值锥体。

<?php
define("URL","http://example.com/mail.php");
$string = 'Page here : {{URL}}'; // string come from database using query //
//$string = str_replace("{{","'.",$string);
//$string = str_replace("}}",".'",$string);
$f = fopen("d:'abc1.txt",'w');
$wr = fwrite($f,$string);
fclose($f);
?>

文件中的输出是:此处页面:{{URL}}

但我需要这样的东西:页面在这里:{{http://example.com/mail.php}}

我还尝试使用注释行替换字符串并将 URL 转换为字符串。

好吧

,如果我做对了,你有一个来自DB的动态字符串,它在某处包含以下内容:"{{URL}}"。所以只需像这样做简单的替换:

define("URL","http://example.com/mail.php");
$string = str_replace('{{URL}}', '{{' . URL . '}}', $string);

这将使用该常量的 URL 值替换 URL 文本

像这样吗?

define("URL","http://example.com/mail.php");
$string = 'Page here : {{' . URL . '}}';

像这样使用语法:

$string = "Page here : {{". URL ."}}"; // string come from database using query //

正如Mikec007所提到的str_replace()在这种情况下将为您工作。