我想从file_get_contents中删除文本


I want to remove text from a file_get_contents

这是我的代码:

<?php include 'parser.php'; 
$api = $_GET['api']; 
$file = file_get_contents("http://example.com");
echo $file; 
?>

这返回

Example text: 111.111.111.111

如何删除示例文本并仅显示 111.111.111.111?

如果你每次都有相同的回报,你可以使用 str_replace 或者你可以使用 explode 并用 : 分解你的字符串。例如:

<?php include 'parser.php'; 
$api = $_GET['api']; 
$file = file_get_contents("http://example.com");
$res = explode(':',$file); 
echo $res[1];
?>

或带str_replace

<?php include 'parser.php'; 
$api = $_GET['api']; 
$file = file_get_contents("http://example.com");
echo str_replace('Example text: ', '', $file);
?>

如果文件中只有此文本,则可以使用str_replace()

$string = $file;
$newString = str_replace('Example text: ', '', $string);
echo $newString;