我有这个测试.php我有这个信息:
callername1 : 'Fernando Verdasco1'
callername2 : 'Fernando Verdasco2'
callername3 : 'Fernando Verdasco3'
callername4 : 'Fernando Verdasco4'
callername5 : 'Fernando Verdasco5'
此页面每 10 分钟自动更改一次该名称
在另一个页面测试中1.php
我需要一个只接受调用者名称3 并回显的 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 "";
}
?>
但没有结果。
还有其他选择吗?
如果我有"="的":"
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;
?>
我需要它来处理":"
帮助?
您应该将数据存储在扩展名为 .php
的文件中,因为它不是可执行的 PHP。我看起来你要的是 JSON 语法。
由于您需要它与":"一起使用,因此我认为,无论出于何种原因,您都无法更改格式。 由于正则表达式,带有"="的示例有效:
preg_match_all('/callername3=''([^'']+)''/',$Text,$Match);
也就是说,匹配文本,如 callername3=
后跟一个'
,后跟一个或多个不是'
的字符,后跟最后一个'
。'
s 之间的所有内容都存储在 $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
,因为看起来您只需要匹配一个示例。
有一个相当简单的方法:
$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
}