总结下最近遇到的一些问题
1.UTF8下PHP截取字符串
function cut_str($sourcestr, $cutlength = 80, $etc = '...')
{
$returnstr = '';
$i = 0;
$n = 0.0;
$str_length = strlen($sourcestr); //字符串的字节数
while ( ($n= 252) //如果ASCII位高与252
{
$returnstr = $returnstr . substr($sourcestr, $i, 6); //根据UTF-8编码规范,将6个连续的字符计为单个字符
$i = $i + 6; //实际Byte计为6
$n++; //字串长度计1
}elseif ( $ascnum >= 248 ) //如果ASCII位高与248
{
$returnstr = $returnstr . substr($sourcestr, $i, 5); //根据UTF-8编码规范,将5个连续的字符计为单个字符
$i = $i + 5; //实际Byte计为5
$n++; //字串长度计1
}elseif ( $ascnum >= 240 ) //如果ASCII位高与240
{
$returnstr = $returnstr . substr($sourcestr, $i, 4); //根据UTF-8编码规范,将4个连续的字符计为单个字符
$i = $i + 4; //实际Byte计为4
$n++; //字串长度计1
}
elseif ( $ascnum >= 224 ) //如果ASCII位高与224
{
$returnstr = $returnstr . substr($sourcestr, $i, 3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
$i = $i + 3 ; //实际Byte计为3
$n++; //字串长度计1
}
elseif ( $ascnum >= 192 ) //如果ASCII位高与192
{
$returnstr = $returnstr . substr($sourcestr, $i, 2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
$i = $i + 2; //实际Byte计为2
$n++; //字串长度计1
}
elseif ( $ascnum>=65 and $ascnum
{
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1; //实际的Byte数仍计1个
$n++; //但考虑整体美观,大写字母计成一个高位字符
}
elseif ( !(array_search($ascnum, array(37, 38, 64, 109 ,119)) === FALSE) ) //%,&,@,m,w 字符按1个字符宽
{
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1; //实际的Byte数仍计1个
$n++; //但考虑整体美观,这些字条计成一个高位字符
}
else //其他情况下,包括小写字母和半角标点符号
{
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1; //实际的Byte数计1个
$n = $n + 0.5; //其余的小写字母和半角标点等与半个高位字符宽...
}
}
if ( $i < $str_length ) { $returnstr = $returnstr . $etc; //超过长度时在尾处加上省略号 } return $returnstr; } ?>
test:
2. (JS问题)select的一些常见js操作方法
//1、向select里添加option
function selectOptionAdd(oSelect, sName, sValue) {
var oOption = document.createElement("option");
oOption.appendChild(document.createTextNode(sName));
if (arguments.length == 3) {
oOption.setAttribute("value", sValue);
}
oSelect.appendChild(oOption);
}
//在select控件的指定位置插入一项
function addOptionAtPosition(oSelect, optionValue, optionText, position) {
if (document.all) //IE
{
var option = document.createElement("option");
option.value = optionValue;
option.innerText = optionText;
oSelect.insertBefore(option, oSelect.options[position]);
}
else { //其他浏览器
oSelect.insertBefore(new Option(optionValue, optionText), oSelect.options[position]);
}
}
//2、删除select里的option
function selectOptionRemoveItem(oSelect) {
if (oSelect.selectedIndex > -1) {//说明选中
for (var i = 0; i < oSelect.options.length; i++) {
if (oSelect.options[i].selected) {
oSelect.remove(i);
i = i - 1; //注意这一行 **************************
}
}
}
}
//select移除一项
function removeOneOption(oSelect, optionValue) {
var selOptions = oSelect.options;
for (var i = 0; i < selOptions.length; i++) {
if (selOptions[i].value == optionValue) {
oSelect.remove(i);
break;
}
}
}
// 清空select所有项目
function removeSelItems(oSelect) {
//删除select中所有项
oSelect.options.length = 0;
}
function del() {
for (var i = s.options.length - 1; i >= 0; i --)
s.options[i] = null;
}
var oSel=document.form1.DropDownCity;
while(oSel.options.length>0){
oSel.removeChild(oSel.options[0]);
}
//3、移动select里的option到另一个select中
function selectsMoveOption(oSelectFrom, oSelectTo) {
for (var i = 0; i < oSelectFrom.options.length; i++) {
if (oSelectFrom.options[i].selected) {
/*if 里的代码也可用下面几句代码代替 var op = oSelectFrom.options[i];oSelectTo.options.add(new Option(op.text, op.value));oSelectFrom.remove(i); */
oSelectTo.appendChild(oSelectFrom.options[i]);
i = i - 1;
}
}
}
//4、select里option的上下移动
function selectMoveUp(oSelect) {
for (var i = 1; i < oSelect.length; i++) {
//最上面的一个不需要移动,所以直接从i=1开始
if (oSelect.options[i].selected) {
/*在进行上下两项互换时,也可以使用以下代码,但是效率很低,因为每一次的Dom操作都将导致整个页面的重新布局,所以不如直接修改元素的属性值。var oOption = oSelect.options[i];var oPrevOption = oSelect.options[i-1];oSelect.insertBefore(oOption,oPrevOption);向下移动同理 */
if (!oSelect.options.item(i - 1).selected) { //上面的一项没选中,上下交换 var selText = oSelect.options[i].text;
var selValue = oSelect.options[i].value;
oSelect.options[i].text = oSelect.options[i - 1].text;
oSelect.options[i].value = oSelect.options[i - 1].value;
oSelect.options[i].selected = false;
oSelect.options[i - 1].text = selText;
oSelect.options[i - 1].value = selValue;
oSelect.options[i - 1].selected = true;
} } } }
function selectMoveDown(oSelect) { for (var i = oSelect.length - 2; i >= 0; i--) {
//向下移动,最后一个不需要处理,所以直接从倒数第二个开始
if (oSelect.options.item(i).selected) {
if (!oSelect.options.item(i + 1).selected) {//下面的Option没选中,上下互换
var selText = oSelect.options.item(i).text;
var selValue = oSelect.options.item(i).value;
oSelect.options.item(i).text = oSelect.options.item(i + 1).text;
oSelect.options.item(i).value = oSelect.options.item(i + 1).value;
oSelect.options.item(i).selected = false;
oSelect.options.item(i + 1).text = selText;
oSelect.options.item(i + 1).value = selValue;
oSelect.options.item(i + 1).selected = true;
}
}
}
}
//5、select里option的排序
/*这里借助Array对象的sort方法进行操作,sort方法接受一个function参数,可以在这个function里定义排序时使用的算法逻辑。*/
//因为排序可以按Option的Value排序,也可以按Text排序,这里按Value排序
function sortItems(oSelect) {
var selLength = oSelect.options.length;
var arr = new Array();
var arrLength;
//将所有Option放入array
for (var i = 0; i < selLength; i++) {
arr[i] = oSelect.options[i];
}
arrLength = arr.length;
arr.sort(sortNumber); //排序
//先将原先的Option删除
while (selLength--) {
oSelect.options[selLength] = null;
}
//将经过排序的Option放回Select中
for (i = 0; i < arrLength; i++) { selectOptionAdd(oSelect, arr[i].text, arr[i].value); //oSelect.add(new Option(arr[i].text,arr[i].value)); } }
//6.鼠标悬浮时获取select的options的index
function getOptionIndex(oSelect) { var theIndex = -1; if (oSelect.options.length > 0) {
theIndex = Math.floor((event.offsetY + 2) / (oSelect.offsetHeight / oSelect.options.length));
if (theIndex < 0) theIndex = 0; else if (theIndex > oSelect.options.length) theIndex = oSelect.options.length;
}
return theIndex;
}