php替换内容的开头以及结尾的换行符

<?php
  header("content-type:text/html;charset=utf-8");
  session_start();
  date_default_timezone_set('PRC');
  $dbinfo = array(
      'host' => 'localhost', //主机名,默认 localhost,本机(mysql+php在一台服务器上)
      'port' => '3306', //端口,默认3306,
      'user' => 'root', //数据库用户名
      'pwd' => 'root', //数据库密码
      'dbname' => 'addressnew'//项目的数据库名称
    );
  function conn($arr) {
    $con = @mysql_connect($arr['host'] . ':' . $arr['port'], $arr['user'], $arr['pwd']);
    if (!$con) {
        exit('链接数据库失败');
    }
    $selectdb = mysql_select_db($arr['dbname'], $con);
    if (!$selectdb) {
        exit('选择数据库失败');
    }
    mysql_query('SET NAMES utf8', $con);
    mysql_query("SET character_set_client=binary", $con);
    return $con;
}
function query($sql,$conn, $num=0) {
    $re = mysql_query($sql, $conn);//执行sql
    
    $sql = strtoupper($sql);//转换成大写
    $tmp = substr($sql, 0, 6);//获取sql字符串中 前 6位         
    if($tmp=='SELECT'){
        $data=array();
        while($row=mysql_fetch_array($re,MYSQL_ASSOC)){
               $data[]=$row;  //把查询到的一行记录保存到数组中
        }   
        if($num==1 && !empty($data)){
            return  $data[0];//从结果中 获取
        }else{
            return $data;//返回二维数组 或 空(没有数据)
        }
    }elseif($tmp=='INSERT'){//insert
        return mysql_insert_id($conn); //返回最后添加的记录自增长字段的值    
    }else{//其他  update delete
          return mysql_affected_rows($conn); //返回值为上一次执行的sql影响的行数
    }
    
}
  $conn = conn($dbinfo); //调函数链接数据库
  //查询经度不是-122或者-123开头的或者纬度不是49开头的同时
  $data=query("select a.id,a.longitude,a.latitude,b.title,b.content from address as a inner join cinfos as b on a.info_id=b.id and b.isyp=1", $conn);

 /* $data1=query("select b.title,b.content,a.id,a.longitude,a.latitude from cinfos as b left join address as a on b.id=a.info_id and b.isyp=1 and a.longitude<=-124 or a.longitude>-122 or a.latitude>=50 or a.latitude<49",$conn);*/
  //替换开头和结尾的<br><br/>/n/r
  function strrep($str){
       $substr1=substr($str,0,4);
       $substr2=substr($str,0,5);
       $substr3=substr($str,0,10);
       $substr4=substr($str,0,11);
       $substr5=substr($str,0,2);
       $substr6=substr($str,-4);
       $substr7=substr($str,-5);
       $substr8=substr($str,-10);
       $substr9=substr($str,-11);
       $substr10=substr($str,-2);
       if($substr1=="<br>"){
         $strn=substr_replace($str,"",0,4);
         $strn=strrep($strn);
       }elseif($substr2=="<br/>"){
         $strn=substr_replace($str,"",0,5);
          $strn=strrep($strn);
       }elseif($substr3=="&lt;br&gt;"){
         $strn=substr_replace($str,"",0,10);
          $strn=strrep($strn);
       }elseif($substr4=="&lt;br/&gt;"){
         $strn=substr_replace($str,"",0,10);
         $strn=strrep($strn);
       }elseif($substr5=="\n"||$substr5=="\r"){
         $strn=substr_replace($str,"",0,2);
          $strn=strrep($strn);
       }elseif($substr6=="<br>"){
         $strn=substr_replace($str,"",-4,4);
          $strn=strrep($strn);
       }elseif($substr7=="<br/>"){
         $strn=substr_replace($str,"",-5,5);
          $strn=strrep($strn);
       }elseif($substr8=="&lt;br&gt;"){
         $strn=substr_replace($str,"",-10,10);
          $strn=strrep($strn);
       }elseif($substr9=="&lt;br/&gt;"){
         $strn=substr_replace($str,"",-10,10);
          $strn=strrep($strn);
       }elseif($substr10=="\n"||$substr5=="\r"){
         $strn=substr_replace($str,"",-2,2);
         $strn=strrep($strn);
       }else{
        $strn=$str;
       }
     return $strn;
  }

  foreach ($data as $key => $value) {
    if($value['longitude']<=-124||$value['longitude']>-122||$value['latitude']>=50||$value['latitude']<49 && !empty($value['latitude']) && !empty($value['longitude'])){
      $value['content']=strrep($value['content']);
     echo 'ID:'.$value['id'].'  经度:'.$value['longitude'].'  纬度:'.$value['latitude'].'  标题:'.$value['title'].'  内容:'.$value['content'].'<hr><br/>';
    }
   
  }


 ?>

//下面是正则的写法

 //\n在双引号是有意义的,在单引号肯定是字符串。
  function strrep($str){
    //$strn = preg_replace("/\r\n/",'',$str);
    $str=str_replace('\n',"<br/>",$str);
    $tmp=$str;
    while($str=preg_replace("/(^(<br\/?>)+)|((<br\/?>)+$)|(^(&lt;br\/?&gt;)+)|((&lt;br\/?&gt;)+$)|(^(\n)+)|((\r)+$)|((\n)+$)/i","",$str)){
      if($str==$tmp){
         break;
      }else{
        $tmp=$str;
      }
    }
   return $str;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值