如何在php中限制数据大小?

时间:2013-09-03 11:50:33

标签: php

我从网页上获取文字内容如下。 有些时候$text中的数据太大,我无法直接将其推送到我的程序中。

那么我可以通过哪种方式限制$text中的数据内容。像1028字节或任何特定的数字

<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$text = file_get_contents($url);
$text=escapeshellarg(strip_tags($text));
?>

3 个答案:

答案 0 :(得分:4)

PHP file_get_content有几个参数,其中一个是要读取的数据大小

请参阅:http://php.net/manual/en/function.file-get-contents.php

例如:

 file_get_contents ( 'http://www.test.com/', false, NULL, -1, 200)

否则,您可以使用cURL,它有更多选项。

答案 1 :(得分:3)

你可以使用内置的php子串函数。

file_get_contents(path,include_path,context,start,max_length)中有5个lenght参数

Check Example#3

//从第21个字符开始读取14个字符

$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);

的var_dump($部);

答案 2 :(得分:2)

如果需要,您可以使用它来限制长度:

<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$text = file_get_contents($url);
$shortText= substr($text, 1024);
$shortText=escapeshellarg(strip_tags($shortText));
?>