PHP preg_match与file_get_contents

时间:2018-01-15 23:48:09

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

我在页面上有一个文本,我想通过使用获取到其他页面 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可以在刷新页面时随时更改

感谢

1 个答案:

答案 0 :(得分:0)

已更新

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()将所有数组元素连接成一个字符串,因此可以将其传递给{{1} }。

希望这会有所帮助:)