Php 从其他网页提取信息

Php 从其他网页提取信息,php,Php,我有这个test.php,其中有以下信息: callername1 : 'Fernando Verdasco1' callername2 : 'Fernando Verdasco2' callername3 : 'Fernando Verdasco3' callername4 : 'Fernando Verdasco4' callername5 : 'Fernando Verdasco5' 此页面每10分钟自动更改一次该名称 这是另一个页面test1.php 我需要一个php代码,它只接受ca

我有这个test.php,其中有以下信息:

callername1 : 'Fernando Verdasco1'
callername2 : 'Fernando Verdasco2'
callername3 : 'Fernando Verdasco3'
callername4 : 'Fernando Verdasco4'
callername5 : 'Fernando Verdasco5'
此页面每10分钟自动更改一次该名称

这是另一个页面test1.php

我需要一个php代码,它只接受callername3的名称并回显它

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 "";
  }

?>
我用这个php代码,它可以工作

<?php 
    $Text=file_get_contents("test.php")
    ;preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match); 
    $fid=$Match[1][0]; 
    echo $fid; 
?>

我需要这个来配合:


帮助?

有一种非常简单的方法来帮助tihs:

$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
}

您应该将数据存储在扩展名为.php的文件中,因为它不是可执行的php。我看你是在使用JSON语法

因为您需要它与“:”一起工作,所以我假定,无论出于何种原因,您都无法更改格式。使用“=”的示例之所以有效,是因为regexp:

preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match); 
也就是说,匹配callername3=之类的文本,后跟一个“后跟一个或多个不是“后跟一个final”的字符”。s之间的所有内容都存储在$Match[1][0]中,如果括号中有更多部分,则它们将存储在$Match[2][0]中,以此类推

您的示例不起作用,因为它没有考虑=符号前后的空格。但我们可以解决这个问题,并将其更改为适用于:如下所示:

preg_match('/callername3\s*:\s*\'([^\']+)\'/',$Text,$Match); 
echo $Match[1] ."\n"; 
这将显示:

Fernando Verdasco3
正则表达式是什么?匹配文本,以callername3开头,后跟任意数量的空格,即\s*后跟a:,后跟任意数量的空格,后跟引号中的名称,存储在$match[1]中,这是括在括号中的正则表达式区域


我还使用了preg_match,因为看起来您只需要匹配一个示例

我希望callername1:“Fernando Verdasco1”不是PHP,因为这是不正确的。什么是$data?AFAIR parse_str仅适用于查询字符串类型格式,您需要一个自定义函数来解析它。@putvande,我可以是PHP的输出。没关系,但是如果信息是这样的“callername3”:“Fernando Verdasco3”我如何解决这个问题?如果你的输入是这样的,你非常接近JSON语法,你应该看看PHP中的JSON_解码。但是,您可以更改regexp以添加引号:/\'callername3\'\s*:\s*\'[^\']+\'/或者,如果引号是可选的/\'?callername3\'?\s*:\s*\'[^\']+\'/
Fernando Verdasco3