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);*/
  //替换开头和结尾的

/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=="
"){
         $strn=substr_replace($str,"",0,4);
         $strn=strrep($strn);
       }elseif($substr2=="
"){
         $strn=substr_replace($str,"",0,5);
          $strn=strrep($strn);
       }elseif($substr3=="<br>"){
         $strn=substr_replace($str,"",0,10);
          $strn=strrep($strn);
       }elseif($substr4=="<br/>"){
         $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=="
"){
         $strn=substr_replace($str,"",-4,4);
          $strn=strrep($strn);
       }elseif($substr7=="
"){
         $strn=substr_replace($str,"",-5,5);
          $strn=strrep($strn);
       }elseif($substr8=="<br>"){
         $strn=substr_replace($str,"",-10,10);
          $strn=strrep($strn);
       }elseif($substr9=="<br/>"){
         $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'].'

';
    }
   
  }


 ?>

//下面是正则的写法

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

你可能感兴趣的:(php替换内容的开头以及结尾的换行符)