首页 > 编程知识 正文

php去掉html格式的简单介绍

时间:2023-12-29 20:32:01 阅读:331063 作者:CNRD

本文目录一览:

php使用正则表达式去掉html中的注释方法

最近在项目中在需要输出浏览器中的源文件需要去掉html中的注释,在网上看了很多的方案,不过很多的答案都是一样的,并不能解决我的问题,于是就自己写正则表达式,也对正则有了更加深刻的理解。

首先比较基础的是:

$a

=

'!--ceshi--ceshi';

$a

=

preg_replace('#!--.*--#'

,

''

,

$a);

var_dump($a);

上面的代码会输出ceshi。

但是如果是下面的字符串的话,就不能达到我们希望的效果了

$a

=

'!--ceshi--ceshi!--ceshi--';

$a

=

preg_replace('#!--.*--#'

,

''

,

$a);

var_dump($a);

于是我们就把匹配规则改成如下的格式

preg_replace('#!--.*?--#'

,

''

,

$a);

但是在html中如果有!--[if

lt

IE

9]ceshi![endif]--这样的代码的话是不能去掉的,所以我们需要改进匹配规则,改成以下的格式

preg_replace('#!--[^![]*?--#'

,

''

,

$a);

又接着如果html中有script!--ceshi//--/script的代码,我们又需要改一下我们的匹配规则了,改成了以下格式

preg_replace('#!--[^![]*?(?!//)--#'

,

''

,

$a);

这样的话我基本上就去掉了我需要去掉的html的注释了!

以上就是小编为大家带来的php使用正则表达式去掉html中的注释方法全部内容了,希望大家多多支持~

php如何清除html格式并去除文字中的空格然后截取文字

PHP清除html、css、js格式并去除空格的PHP函数

01 function cutstr_html($string,$length=0,$ellipsis='…'){

02 $string=strip_tags($string);

03 $string=preg_replace('/n/is','',$string);

04 $string=preg_replace('/ | /is','',$string);

05 $string=preg_replace('/ /is','',$string);

06 preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/",$string,$string);

07 if(is_array($string)!empty($string[0])){

08 if(is_numeric($length)$length){

09 $string=join('',array_slice($string[0],0,$length)).$ellipsis;

10 }else{

11 $string=implode('',$string[0]);

12 }

13 }else{

14 $string='';

15 }

16 return $string;

17 }

php 去除html标签 js 和 css样式

01 function clearHtml($content){

02 $content=preg_replace("/a[^]*/i","",$content);

03 $content=preg_replace("//a/i","",$content);

04 $content=preg_replace("/div[^]*/i","",$content);

05 $content=preg_replace("//div/i","",$content);

06 $content=preg_replace("/!--[^]*--/i","",$content);//注释内容

07 $content=preg_replace("/style=.+?['|"]/i",'',$content);//去除样式

08 $content=preg_replace("/class=.+?['|"]/i",'',$content);//去除样式

09 $content=preg_replace("/id=.+?['|"]/i",'',$content);//去除样式

10 $content=preg_replace("/lang=.+?['|"]/i",'',$content);//去除样式

11 $content=preg_replace("/width=.+?['|"]/i",'',$content);//去除样式

12 $content=preg_replace("/height=.+?['|"]/i",'',$content);//去除样式

13 $content=preg_replace("/border=.+?['|"]/i",'',$content);//去除样式

14 $content=preg_replace("/face=.+?['|"]/i",'',$content);//去除样式

15 $content=preg_replace("/face=.+?['|"]/",'',$content);//去除样式 只允许小写 正则匹配没有带 i 参数

16 return $content;

17 }

php中使用正则表达式或其他方法去除html标签的样式属性(不能写死的)

preg_replace('/([a-z]+)[^]*/is', '\1', $str);

我使用的就是这个正则。

php截取字符串以及去掉html标记

第一个很简单,就用你提供这个函数就可以。

不过第二个没有函数能执行。因为如果是英文字符应该占用1位,但中文不一样,utf8的一个汉字占用3个字符,这样如果出现中英文一起的情况就会出现乱码。必须自定义函数解决,下面给你一个简单的

?php

function chinesesubstr($str,$start,$len) {//$str是指字符串,$start指字符串的起始位置,$len指字符串长度

$strlen=$start+$len; //用$strlen存储字符串的总长度

for($i=0;$i$strlen;$i++) {

if(ord(substr($str,$i,1))0xa0) { //如果字符串中出现汉字,也就是ASC码大于0xa0的。作出判断与英文字符不一样。

$tmpstr.=substr($str,$i,2);

$i++;

}

else

$tmpstr.=substr($str,$i,1);

}

return $tmpstr;

}

?

PHP 如何获取当前URL并去掉.html

很简单啊:

?php

//

第一步:你需要得到你的URL:

$URL=$_SERVER['HTTP_HOST']

.

$_SERVER['REQUEST_URI'];

//第二步:把得到的URL后面的“.HTML”去掉:

$geturl=str_replace('.html','',$URL);

echo

$geturl;

?

但是,可但是:

你的这个页面应该是php的才对吧,如果是html的,就一定是应用了拟静态技术来重写URL,这样的话,上面的代码你也可以使用,如果是生成的

静态页面

,那么很不好意思,这个基本上不可行了。也无法达到你的意愿。

php 过滤掉html标签及标签内的所有内容

方法一:使用strip_tags()函数

strip_tags() 函数剥去字符串中的 HTML、XML 以及PHP的标签。

使用案例:

$string = "p这里是潘旭博客/p"

$newStr = strip_tags($string);

echo $newStr;

方法二:使用str_replace()函数

str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)

使用案例:

$string = "p这里是潘旭博客/p";

$newStr = str_replace(array("p","/p"),array("",""));

echo $newStr;

另外还有一种是通过正则的方法,请参考:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。