删除字符串中多余的换行

function remove_blank_lines($content) {
	$result = explode("\n", $content);
	for ($i = 0; $i < count($result); $i ++) {
		if (trim($result[$i]) != "") {
			$new_content .= $result[$i];
			$new_content .= "\n";
		}
	}
	return $new_content;
}

很笨的方法,不是么~
写这个过程中还明白了单双引号的区别,真是惭愧。。。

底下大大给的高级方法:

function remove_blank_lines($content) {
	return preg_replace("/(\s*?\r?\n\s*?)+/","\n",$content);
}

配合这里学习正则表达式。

17fav 收藏本文
标签:, ,

相关日志 随机文章

Comments

5 Responses to “删除字符串中多余的换行”

  1. Nameless on 2006-11-01 9:56 pm


    function remove_blank_lines ($content) {
    return preg_replace("/(\s*?\r?\n\s*?)+/","\n",$content);
    }

    这个如何?

    [Reply]

  2. Sunshow on 2006-11-02 9:12 am

    果然好使
    高人啊
    不怎么会正则的说。。

    [Reply]

  3. bianbian on 2006-11-02 2:10 pm

    说实话,ss要补补课了。正则真是好东西,要是找到发明正则的(是不是Perl的作者?)我一定给他捐款。

    [Reply]

  4. Nameless on 2006-11-02 11:45 pm

    正则不是Perl作者发明的,可能是某批科学家的某项科学研究的副产物,
    PHP里面的Pxx系列正则处理函数是一套POSIX兼容的函数

    [Reply]

  5. Serkan on 2006-12-17 3:34 am

    is that all?

    [Reply]

Leave a Reply




请输入验证码

Use "<coolcode></coolcode>" to publish your code.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.

京ICP备05059555号

收藏 & 分享

Powered by 17fav.com