我正在尝试使用file_get_contents()从互联网上删除额外的字符串空格。我尝试了str_replace() andpreg_replace(),也进行了搜索,但它们都没有工作。
这是我的代码:
<?php $html_content = file_get_contents("http://mindcity.sina.com.hk/MC-lunar/daily/2014/12/20141209_b5.html");
$html_content = mb_convert_encoding($html_content, 'UTF-8', 'BIG-5');
$html_content = strip_tags($html_content);
$start_pos = strrpos($html_content, "宜 :");
$end_pos = strrpos($html_content, "凶神宜忌 :") - strlen($html_content);
$good_to_do = substr($html_content, $start_pos, $end_pos);
echo $good_to_do .'<br>';
//remove whitespace of $good_to_do
$good_to_do = str_replace(' : ','*',$good_to_do);
$good_to_do = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $good_to_do);
$good_to_do = str_replace(array("\r\n", "\r", "\n", "\t", "\0", "\s", "\x0B", "\x20", "\xA0"), '*', $good_to_do);
var_dump( $good_to_do ); ?>发布于 2015-01-27 12:30:05
刚刚发现,当查看源时,空白是 。所以代码只是简单地变成了str_replace(' ', '', $html_content);
发布于 2014-12-10 07:52:26
做
$good_to_do = preg_replace('/\s+/', '*', $good_to_do);我写了“*”,因为这就是你想用的?你可以把任何你喜欢的东西放在那里。
https://stackoverflow.com/questions/27395156
复制相似问题