① php去掉<div id="content" style=" width:100px"></div>中的內容,就保留<div></div>
<?php
$str = '<div id="content" style=" width:100px">contents</div>';
$output = preg_replace('/<div[^>]*>/i' , '<div>' , $str);
echo htmlspecialchars($output);
?>
//============================================================
<?php
$ary_html = array(
'div' => '<div id="content" style=" width:100px"></div>'
,'span' => '<span id="s" style=" width:100px"></span>'
,'td' => '<td id="sd" style=" width:100px"></td>'
,'tr' => '<tr id="aaa" style=" width:100px"></tr>'
,'th' => '<th id="cc" style=" width:100px"></th>'
,'body' => '<body id="ddd" style=" width:100px"></body>'
,'body' => '<body id="dafa" style=" width:100px"></body>'
,'html' => '<html xmlns="http://www.w3.org/1999/xhtml"></html>'
,'head' => '<head id="head"></head>'
,'title' => '<title al=\'asd\'>this is the content</title>'
);
$reg = '/(<\w+)[^<]+>/im';
foreach($ary_html as $key => $val){
$str = preg_replace($reg , "$1>" , $val);
echo "<li>$key : " , color( htmlspecialchars($val) , 'red') , ' => ' , color(htmlspecialchars($str) , 'green');
}
function color($html , $color){
return "<font color=$color>" . $html . '</font>';
}
?>
② php正則去掉<img>標簽里的height和width
"img*****src"
替換成 "img src"
"width=570 border"
替換成 " border"
③ php數據控制css中的width大小
width屬性的使用方法進行總結。
width屬性的使用
1. 解析width:100%;與width:auto;的區別
如果是p的width:100%,則說明p的width會得到980px就已經充滿p區域,然後自己又有padding,所以會超出p。而當width:auto時它是總體寬度(包括width,margin,padding,border)等於父級寬度(width,不包含父級的margin,padding,border),所以如果padding已經左右佔去10px的空間,那麼width給的值就是960px。但無論是width:100%還是auto,其計算的參照都是父級內容區width值,而非總寬度值.
2. CSS調節圖片的大小,要知道width、height
如何用css調整圖片的大小,使用width和height這兩個屬性,這兩個屬性分別是用來設置元素的寬度和高度的。在CSS初學階段,全部都是使用像素作單位。
3. css
寬度
width
寬度都是直接設置標簽對象寬度,並且注意「等號」後跟具體數字寬度值(或百分比),具體寬度值不用跟長度單位,默認以像素(px)為單位,在TABLE表格標簽或圖片img標簽內設置寬度時候其值不跟html單位,默認以像素為單位。
4. CSS2.1SPEC:視覺格式化模型之width屬性詳解(上)
width屬性有幾種不同的取值方式,有幾下幾種:
(1)寬度值:也就是用CSS長度值來明確地規定一個盒子的寬度,取值單位可以用CSS中支持的長度單位,比如px,em等。
(2)百分比:百分比是根據
包含塊的寬度來計算的。但是如果包含塊的寬度需要根據包含的內容來決定,那麼最終的布局是未定義的,也就是標准中沒有明確規定如何計算寬度值。另外,如果盒子是絕對定位的,那麼它的百分比長度在計算時是根據包含塊的
padding
edge的寬度來計算的。
(3)auto:根據CSS標准中規定的寬度計算方法來計算,具體的演算法下文中將詳細介紹
(4)inherit:事實上width屬性是不可繼承的,很奇怪為什麼會有inherit這個選項,實際情況中width屬性一般也很少用到inherit值
5. CSS2.1SPEC:視覺格式化模型之width屬性詳解(下)
"shrink-to-fit"演算法的計算過程:
[1]計算preferred-width:在
除非包含的內容有明確的換行符(比如有<br/>標簽時),否則就不換行的情況下,容納其包含的內容所需要的寬度。
[2]計算preferred-min-width:在
能換行時(英文碰到空格或標點符號,出現了塊級元素,當然也包含出現了<br/>標簽時)就換行的情況下,容納所包含的內容需要的寬度。
[3]計算available-width:即利用2.2.3節中的公式:
available-width
=width
of
containing
block
-
'margin-left'
-
'border-left-width'
-
'padding-left'
-
'padding-right'
-
'border-right-width'
-
'margin-right,這里也包括所有滾動出去的寬度。
最終的width則為:min(preferred-width,
max(preferred-min-width,
available-width))。最終的公式可以總結為:最終的寬度以available-width為基準,同時保證不能大於preferred-width以及不能小於preferred-min-width
④ PHP 根據判斷大容器的width,輸入小容器
如果你這個寬度是按照客戶端的來調整的話這個在PHP里是沒法做的.你只能變通到其他的方法.
用JS動態調整每個LI的寬度可以做到.
當你的大窗口是750的時候,你可以把該容器下的class設定為li1
.li1{width:275px; margin:1px;}
.li2{width:180px;margin:1px;}
在PHP里你的LI是一直下支的.
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
JS判斷出大容器的寬度後就會對LI進行自動調整,就能夠自適應顯示了.
思路就是這樣,實現也不難.
⑤ php匹配<img/>,添加width,height
這個問題你想復雜了,其實直接在前台用CSS樣式控制就可以了。
比如你的通過編輯器編輯的內容最終在一個類樣式為.content的DIV中顯示,則添加樣式
.content img{width:100px;height:100px;border:0px;}
就可以控制這個DIV下所有的圖像了,沒必要去程序中處理。
或者通過JS控制也是可行的方法.
假設顯示這些圖片的DIV容器的ID是content
<div id="content"></div>
<script language="javascript">
function DrawImage(ImgD,w,h){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>1){
if(image.width>w){
ImgD.width=w;
ImgD.height=(image.height*w)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>h){
ImgD.height=h;
ImgD.width=(image.width*h)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
var images =document.getElementById("content").getElementsByTagName("img");
for(var i=0;i<images.length;i++){
var img =images[i];
DrawImage(img,100,100);
}
</script>
⑥ php正則保留style=「width=」其他全部刪除
$str = '<table style="width:181pt;height:222px;***********"></table>';
$pattern = "/ style=\".*?\"/";
$replacement = "";
echo preg_replace($pattern, $replacement, $str);
⑦ PHP div 寬度怎麼改變
給.subheader_right 加個寬度 width:000px;
⑧ php算出的值賦給style的width值怎麼寫
echo'<imgstyle="width:'.$width.'px"/>';
⑨ php定界符中{變數}是什麼意思
php 中 如果是雙引號 php會去判斷 裡面是否有php變數,而{}是方便讓php更快捷查找,好像低版本的php不加的話會報錯。
如果是單引號 則不判斷,直接認定為字元串
⑩ php獲取當前頁面寬度
JQ行嗎?
$(document).width();然後傳到後台接收咯
手冊ajax事件
這個是簡單的例子:
JS:
<scritp>
$(function(){
varH=$(document).width();
$.post("test.php",{h:H},function(data){
alert(data);
});
});
</script>
這里假設html和php是同目錄
test.php
<?php
$h=$_POST['h'];//這個就是接收的參數
echo$h;die;
?>
//具體的就不說明了,代碼運行一次就明白了,訪問html文件