【问题标题】:PHP preg_match with file_get_contentsPHP preg_match 与 file_get_contents
【发布时间】:2018-01-15 23:48:09
【问题描述】:

我在一个页面上有一个文本,我想通过使用来获取其他页面 preg_matchfile_get_content 函数

page1.php 上的文字是

return(["h","t","t","p",":","\/","\/","1","7",".","2","4","8",".","1","7","4",".","3","8",":","8","0","8","1","\/"])

我想像这样从page1.phppage2.php 获取文本

IP是:http://17.248.174.38:8081/

IP现在自动是17.248.174.38:8081,刷新后可以变成133.213.171.33:8081所以是从page1生成的

我试过这段代码,但它不起作用

   <?php
    $url = "http://localhost/page1.php";
    $contents = file_get_contents($url);
    preg_match('/(http?\:\/\/[^\']*\.8081*)/', $contents, $result);
    $link = urldecode($result[0]);
    $echo "The IP is :" $link;
    ?>

请帮忙,我只是php的初学者

IP 17.248.174.38 可以在任何时候刷新页面进行更改

谢谢

【问题讨论】:

  • IP 17.248.174.38:8081 可以自动更改为任何其他 ip

标签: php expression preg-match file-get-contents


【解决方案1】:

更新

在 page2.php 中使用这个

<?php
$url = "page1.php"; // use file location in refrence of the current file. 
$contents = file_get_contents($url);
preg_match('/return\(\[\"h\",\"t\",.+\]\)/i', $contents, $result);
$result = json_decode(trim(str_replace("return","",$result[0]),"()"));
$link = urldecode($result);
$echo "The IP is :" $link;
?>

json_decode()用于解码和解析响应(这是一个JSON数组),然后implode()将所有数组元素连接成一个字符串,因此可以将它传递给preg_match()

希望这会有所帮助:)

【讨论】:

  • 谢谢,但是在 page1.php 上,我每次刷新页面的新 IP,所以第一次可以返回([“h”,“t”,“t”,“p”,,: ","/","/","1","7",".","2","4","8",".","1","7","4", ".","3","8",":","8","0","8","1","/"]) ,第二次刷新可以return(["h"," t","t","p",":","/","/","1","4",".","2","1","8","." ,"1","7","6",".","3","8",":","8","0","8","1","/"])
  • 所以我真正想要的是得到回报([从这个到这个])并将它放在 preg_match 上
  • file_get_contents() 返回return(["h","t","t","p",":","/","/","1","7",".","2","4","8",‌​".","1","7","4",".",‌​"3","8",":","8","0",‌​"8","1","/"]) ??
  • 在 page1.php 上有很多文本代码,但我只想得到返回值的内容,在这个例子中 return(["h","t","t"," p",":","/","/","1","7",".","2","4","8",".","1","7" ,"4",".","3","8",":","8","0","8","1","/"]) 我想得到什么返回并显示在 page2.php 上,如17.248.174.38:8081
  • 更新了答案
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2012-06-19
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多