我正在编写一个脚本,它返回位于php
文件顶部的$ title变量的文字值。在脚本的最后,我使用rtrim()
来删除字符串末尾的引号和分号,但是它们不会修剪。我的php
文件的顶部如下所示:
<php
$title="Test Title";
$description="Test Description";
?>
当我回复字符串时,我得到:
Test Title;"
这是我的代码。谁能告诉我我做错了什么?我甚至欢迎任何有关改善这一点的建议:
<?php
//returns the value of the $title variable from the top of a php file.
$file = fopen("test.php", "r") or die("Unable to open file!");
$count = 0;
while ($count < 10) { //only check the first 10 lines
$line = fgets($file);
$isTitle = strpos($line, "itle="); //check if $title is part of the string
if ($isTitle !== false) {
$fullTitle = explode("=\"", $line); //explode it into two on =" which also trims the first quote
$untrimmedTitle = $fullTitle[1]; //save the second part of the array as a string since rtrim needs a string
$title = rtrim($untrimmedTitle, "\";"); //trim the quote and semi-colon from the string
$count = 10; //push the count up to 10 so it ends the loop
}
$count++;
}
echo $title; //show the title
fclose($file);
?>
答案 0 :(得分:2)
在行中:
$title = rtrim($untrimmedTitle, "\";");
您正在从字符串中调查"
和;
。您想修剪'
和;
:
$title = rtrim($untrimmedTitle, "\';");
修改强>
为什么不这样做:
include "test.php";
echo $title;
答案 1 :(得分:0)
将$title = rtrim($untrimmedTitle, "\";");
更改为$title = rtrim(trim($untrimmedTitle), "\";");
。没关系。
在$ untrimmedTitle结束时有一个像\n
这样的休息。
答案 2 :(得分:0)
或者你可以这样做:
$title = rtrim($untrimmedTitle, "\";\n");