首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法删除php中从internet检索到的字符串中的额外空格。

无法删除php中从internet检索到的字符串中的额外空格。
EN

Stack Overflow用户
提问于 2014-12-10 07:10:11
回答 2查看 1.2K关注 0票数 0

我正在尝试使用file_get_contents()从互联网上删除额外的字符串空格。我尝试了str_replace() andpreg_replace(),也进行了搜索,但它们都没有工作。

这是我的代码:

代码语言:javascript
复制
<?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 ); ?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-27 12:30:05

刚刚发现,当查看源时,空白是&nbsp;。所以代码只是简单地变成了str_replace('&nbsp;', '', $html_content);

票数 0
EN

Stack Overflow用户

发布于 2014-12-10 07:52:26

代码语言:javascript
复制
$good_to_do = preg_replace('/\s+/', '*', $good_to_do);

我写了“*”,因为这就是你想用的?你可以把任何你喜欢的东西放在那里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27395156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档