我有这个test.php,我有这个信息:
callername1 : 'Fernando Verdasco1'
callername2 : 'Fernando Verdasco2'
callername3 : 'Fernando Verdasco3'
callername4 : 'Fernando Verdasco4'
callername5 : 'Fernando Verdasco5'
此页面每10分钟自动更改一次该名称
在另一页test1.php
中我需要一个只带有callername3和echo'it
名称的php代码Fernando Verdasco3
我试过这样就像test1.php?id = callername3
<?php
$Text=file_get_contents("test.php");
if(isset($_GET["id"])){
$id = $_GET["id"];
parse_str($Text,$data);
echo $data[$id];
} else {
echo "";
}
?>
但没有结果。
还有其他选择吗?
如果我有“=”instade“:”
callername1 = 'Fernando Verdasco1'
callername2 = 'Fernando Verdasco2'
callername3 = 'Fernando Verdasco3'
callername4 = 'Fernando Verdasco4'
callername5 = 'Fernando Verdasco5'
我使用这个PHP代码工作
<?php
$Text=file_get_contents("test.php")
;preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid;
&GT;
我需要这个用“:”
帮助?
答案 0 :(得分:0)
有一个相当简单的方法:
$fData = file_get_contents("test.php");
$lines = explode("\n", $fData);
foreach($lines as $line) {
$t = explode(":", $line);
echo trim($t[1]); // This will give you the name
}
答案 1 :(得分:0)
您应该将数据存储在.php
扩展名的文件中,因为它不是可执行的PHP。我看起来你正在寻找JSON语法。
因为你需要它来使用':'我假设,无论出于何种原因,你都无法改变格式。使用'='的示例因正则表达式而起作用:
preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match);
这就是说,匹配callername3=
后跟'
后跟一个或多个不是'
后跟最后'
的字符的文字。 '
之间的所有内容都存储在$ Match [1] [0]中(如果括号中有更多部分存储在$ Match [2] [0]等中)。
您的示例不起作用,因为它没有考虑=
符号之前和之后的空格。但是我们可以解决这个问题并将其更改为:
,就像这样:
preg_match('/callername3\s*:\s*\'([^\']+)\'/',$Text,$Match);
echo $Match[1] ."\n";
显示:
Fernando Verdasco3
正则表达式是匹配文本的开头callername3
后跟任意数量的空格(即\s*
)后跟:
,后跟任意数量的空格,其次是通过引号中的名称(存储在$ Match [1]中,这是括号中括起来的正则表达式的区域)。
我还使用了preg_match
,因为看起来你只需要匹配一个例子。