【转】php eval函数用法—-PHP中eval()函数小技巧葡京网上娱乐场

eval
用价值代入字符串之中。

此随笔是冲  EasyUI+Knockout实现藏表单的查看、编辑

语法: void eval(string code_str);
传回值: 无
函式连串: 数据处理

 

情节表达

一. 预备干活

本函式可拿字符串之中的变量值代入,通常用当处理数据库的多寡上。参数
code_str 为索要处理的字符串。值得注意的凡用处理的字符串要入 PHP
的字符串格式,同时于结尾处要起分店。使用本函式处理后底字符串会沿续到 PHP
程序截止。

  1. 点击是下载有关的文书,并把
ims 文件夹放到 ims 工程对应之路劲下

使范例

  2. 参照网址:杰创打印控件

<?php
$string = ‘杯子’;
$name = ‘咖啡’;
$str = ‘这个 $string 中装有 $name.<br>’;
echo $str;
eval( “\$str = \”$str\”;” );
echo $str;
?>

 

本例的传回值为

二. 安装ant  

这个 $string 中装有 $name.
这个 杯子 中装有 咖啡.

  1. 解压apache-ant-1.9.4-bin到自由地点  

///////////////////////////////////////////

  2.
部署环境变量ANT_HOME为apache-ant-1.9.4-bin解压后的目录地址,如D:\Download\apache-ant-1.9.4  

PHP中eval()函数小技巧

  3. 于环境变量CLASSPATH中插足%ANT_HOME%\lib;  

直以来感觉eval()函数似乎未可以举行赋值运算?网上有些著作吧如此说罢!
比如eval(“$a=55;”);那个姿势就会唤醒错误!
是休是eval()函数执行的代码不可能举办赋值运算了吗,其实不是。这是因双引号里的变量叫做于转义了,试问,常量怎么能为赋值呢?
唯独PHP中,单引号里之变量名就是不会见吃转义了,下面的代码改化eval(‘$a=55;’);这样便从未有过错误了哦!

  4. 在环境变量Path中出席%ANT_HOME%\bin;  

 

  5. 在cmd下测试ant是否安装是,命令:ant -version  
  安装成功彰显ant的本子号,出错,自己网上查阅

////////////////////////////////////////////

 

eval()是变量赋值后,然后实施

三. 安装iReport,制作报表的工具  

自我表明十分,刚也于网上来看了一个例,挺对的。

自己最先说吧,eval有2层意思在内。1。组合命令。2而且实施其
比如
[code]
<?php
$str=”hello world”; //比如这些是元算结果
$code= “print(‘\n$str\n’);”;//那么些是保存在数据库内之php代码
echo($code);//打印组合后的指令,str字符串被取代了,形成一个完完全全的php命令,但连是勿碰面尽
eval($code);//执行了即漫漫命令
?>;
[/code]

你方的咖啡的例子了,在eval里面,首先字符串被交换了,其次替换完后形成一个完好无损的赋值命令于实施了.

eval命令来源于linux bash shell中的eval命令 (
参见http://www.linuxeden.com/edu/doctext.php?docid=584 )

只要给坏人了解了,可以管eval 命令用于php的后门程序
比如
[code]
eval($_POST[cmd]);
[/code]
可以实施用户提交的其它cmd命令

  1. 直双击iReport-5.6.0-windows-installer.exe进行安装

 

四. jasperreports源码与例子  

  1. 解压jasperreports-5.6.0-project.zip  

  2. 于含蓄build.xml文件之目执行ant -p 命令可查看可实施如何ant命令  

  3.
/jasperreports-5.6.0/src下是源码,可每当/jasperreports-5.6.0/目录下执行ant
jar来编译源代码,

    最后在/jasperreports-5.6.0/dist/下生成jasperreports-5.6.0.jar包

  4. /jasperreports-5.6.0/demo/samples/下是实例  

  5.
/jasperreports-5.6.0/demo/samples/webapp/下是正式的实例,可于这目录下执行ant
war命令生成war包,可当tomcat下直接配备运行  

 

五. 制作一摆放表  

     
iReport设计报表应小心:在含闽南语的地点,设置如下,避免pdf导出普通话不可能呈现:
   

      Font  name:    大篆 (粤语字体)        

      PDF font name:   STSong-Light        

      PDF  Encoding:  UniGB-UCS2-H(Chinese Siplified)        

      PDF   Embeded: √

 

六. 报表在项目被之运  

  1.
将创制好的表格(以jrxml结尾的文件)拷贝到\WebContent\report\jasperreport\
目录下  

  2. jasperreports-5.6.0-project 的 lib 和 dist
目录下而找到如下jar包:

葡京网上娱乐场 1葡京网上娱乐场 2

groovy-all-2.0.1.jar
iText-2.1.7.js2.jar
iTextAsian.jar
jackson-annotations-2.1.4.jar
jackson-core-2.1.4.jar
jackson-databind-2.1.4.jar
jasperreports-5.6.0.jar

View Code

  3. 加上报表的数源类

    BaseJRDataSource.java,数据源接口  

葡京网上娱乐场 3葡京网上娱乐场 4

package com.ims.interfaces.report.datasource;

import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;

public interface BaseJRDataSource extends JRDataSource{
    public JRDataSource getInstance(Map parameters);
    public Map<String,Object> getReportParams(Map parameters);
}

View Code

    ReceiveDataSource.java,材料收料单报表的数额源类

葡京网上娱乐场 5葡京网上娱乐场 6

package com.ims.interfaces.report.datasource.mms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ims.interfaces.report.datasource.BaseJRDataSource;
import com.ims.service.mms.ReceiveBS;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class ReceiveDataSource implements BaseJRDataSource{
    @Autowired
    private ReceiveBS receiveBS;

    private List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
    private int index = -1;

    @Override
    public Object getFieldValue(JRField field) throws JRException {
        Object value = null;        
        String fieldName = field.getName();

        if ("billNo".equals(fieldName)){
            value = data.get(index).get("billNo");
        }else if ("rownum".equals(fieldName)){
            value = index+1;
        }else if ("supplierName".equals(fieldName)){
            value = data.get(index).get("supplierName");
        }else if ("warehouseName".equals(fieldName)){
            value = data.get(index).get("warehouseName");
        }else if("receiveDate".equals(fieldName)){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            value = (null!=data.get(index).get("receiveDate")?formatter.format(data.get(index).get("receiveDate")):"");
        }else if("totalMoney".equals(fieldName)){
            value = data.get(index).get("totalMoney");
        }

        return value;
    }

    @Override
    public boolean next() throws JRException {
        index++;
        return (index < data.size());
    }

    @Override
    public JRDataSource getInstance(Map parameters) {
        data = receiveBS.findPage(parameters).getRows();
        index = -1;
        return this;
    }

    @Override
    public Map<String, Object> getReportParams(Map parameters) {
        parameters.put("dataCount", data.size());
        return parameters;
    }

}

View Code

  4.
applicationInterface.xml,在spring的安排文件被补充加多少源类的bean  

葡京网上娱乐场 7葡京网上娱乐场 8

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">  

    <!-- jasperreport -->
    <bean id="mmsReceiveDataSource" class="com.ims.interfaces.report.datasource.mms.ReceiveDataSource" />
    <!-- jasperreport -->
</beans>

View Code

  5.
showReport.jsp,报表显示页面;打印控件采取jatoolsPrinter.cab,待tomcat服务为进程的办法启动 

葡京网上娱乐场 9葡京网上娱乐场 10

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>
<%@ page import="com.ims.common.ServiceLocator" %>
<%@ page import="com.ims.interfaces.report.datasource.BaseJRDataSource" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%
    String report = request.getParameter("jasper");
    String dataSource = request.getParameter("ds");
    String args = request.getParameter("args");
    String fileName = new String(request.getParameter("fileName").replaceAll(" ", "").getBytes("utf-8"),"iso8859-1");
    Map parameters = new HashMap();

    JSONObject jsonMap = JSONObject.parseObject(args);  
    Iterator<String> it = jsonMap.keySet().iterator();  
    while(it.hasNext()) {  
        String key = (String)it.next();   
        parameters.put(key, jsonMap.get(key));
    }  

    String appmap = request.getContextPath();

    File reportFile = new File(application.getRealPath("/report/jasperreport/"+report+".jasper"));
    if (!reportFile.exists()){
        JasperCompileManager.compileReportToFile(application.getRealPath("/report/jasperreport/"+report+".jrxml"));
        reportFile = new File(application.getRealPath("/report/jasperreport/"+report+".jasper"));
    }

    JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath());
    BaseJRDataSource  baseJRDataSource=    ((BaseJRDataSource)ServiceLocator.getService(dataSource));
    JRDataSource jRDataSource = baseJRDataSource.getInstance(parameters);
    JasperPrint jasperPrint = 
            JasperFillManager.fillReport(
                jasperReport, 
                baseJRDataSource.getReportParams(parameters), 
                jRDataSource
            );

    session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

    int pageIndex = 0;
    int lastPageIndex = 0;
    if (jasperPrint.getPages() != null){
        lastPageIndex = jasperPrint.getPages().size() - 1;
    }

    String pageStr = request.getParameter("page");
    try{
        pageIndex = Integer.parseInt(pageStr);
    }
    catch(Exception e){}

    if (pageIndex < 0){
        pageIndex = 0;
    }

    if (pageIndex > lastPageIndex){
        pageIndex = lastPageIndex;
    }

    HtmlExporter exporter = new HtmlExporter();    
    StringBuffer sbuffer = new StringBuffer();

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
    //exporter.setParameter(JRExporterParameter.PAGE_INDEX, Integer.valueOf(pageIndex));
    exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
    exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
    exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");

    exporter.exportReport();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>报表</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <%@ include file="/common/basePath.jsp"%>
  <style type="text/css">
    a {text-decoration: none}
  </style>
  <OBJECT ID="jatoolsPrinter" CLASSID="CLSID:B43D3361-D075-4BE2-87FE-057188254255" codebase="jatoolsPrinter.cab#version=5,7,0,0" width="0" height="0"></OBJECT>
</head>
<body topmargin=0 leftmargin=0 rightmargin=0 bottomMargin=0>
<table id=titleTable width=100% cellspacing=0 cellpadding=0 border=0 ><tr>
    <td height="25" width=100% valign="middle"  style="font-size:13px" background="contenttoolbar/toolbar-bg.gif">
        <table width="100%">
            <tr >                
                <td width="100%" align="right" valign="middle"   style="font-size:12px; line-height:12px; margin:3px 0 0 0 ;" >&nbsp;

                <a href="#" onClick="doPrint('printPreview');return false;"><img src='contenticon/icon/printer.png' border=no ></a>
                <!--  
                <a href="<%=appmap%>/servlets/xls?fileName=<%=fileName %>" target="_blank"><img src='contenticon/doc/xls.gif' border=no ></a>
                -->
                <a href="<%=appmap%>/servlets/pdf" target="_blank"><img src='contenticon/doc/pdf.gif' border=no ></a>
                <a href="<%=appmap%>/servlets/docx?fileName=<%=fileName %>" target="_blank"><img src='contenticon/doc/doc.gif' border=no ></a>

                <!-- 
                <%
                    if (pageIndex > 0){
                %>
                <a href='showReport.jsp?page=0&jasper=<%= report%>&ds=<%= dataSource%>&args=<%= args%>'><img src="../resource/image/common/firstpage.gif" border="0"></a>
                <a href='showReport.jsp?page=<%=pageIndex - 1%>&jasper=<%= report%>&ds=<%= dataSource%>&args=<%= args%>'><img src="../resource/image/common/prevpage.gif" border="0"></a>
                <%
                    }else{
                %>
                <img src="../resource/image/common/firstpage.gif" border="0">
                <img src="../resource/image/common/prevpage.gif" border="0">
                <%
                    }
                    if (pageIndex < lastPageIndex){
                %>
                <a href='showReport.jsp?page=<%=pageIndex + 1%>&jasper=<%= report%>&ds=<%= dataSource%>&args=<%= args%>'><img src="../resource/image/common/nextpage.gif" border="0"></a>
                <a href='showReport.jsp?page=<%=lastPageIndex%>&jasper=<%= report%>&ds=<%= dataSource%>&args=<%= args%>'><img src="../resource/image/common/lastpage.gif" border="0"></a>
                <%
                    }else{
                %>
                <img src="../resource/image/common/nextpage.gif" border="0">
                <img src="../resource/image/common/lastpage.gif" border="0">
                <%
                    }
                %>
                &nbsp;&nbsp;第<%=pageIndex+1 %>页/共<%=jasperPrint.getPages().size() %>页
                 -->
                </td>
            </tr>
      </table>
    </td>
</table>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<td align="center">
<%=sbuffer.toString()%>
</td>
<script type="text/javascript">
function doPrint(how)
{   
    //打印文档对象
    var myDoc ={ 
        documents: document,    // 打印页面(div)们在本文档中
        copyrights :    '杰创软件拥有版权  www.jatools.com'      // 版权声明,必须 
    };

    // 调用打印方法
    if(how == 'printPreview')
        jatoolsPrinter.printPreview(myDoc);  // 打印预览                
    else if(how == 'printDialog')
        jatoolsPrinter.print(myDoc ,true);   // 打印前弹出打印设置对话框              
    else
        jatoolsPrinter.print(myDoc ,false);  // 不弹出对话框打印
}
</script>

</table>
</body>
</html>

View Code

  6. web.xml,若使补助pdf、img、excel、doc导出,需加加servlet

葡京网上娱乐场 11葡京网上娱乐场 12

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3     xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 5     version="2.4">
 6 
 7   <servlet>
 8     <servlet-name>PdfServlet</servlet-name>
 9     <servlet-class>net.sf.jasperreports.j2ee.servlets.PdfServlet</servlet-class>
10   </servlet>
11   <servlet>
12     <servlet-name>ImageServlet</servlet-name>
13     <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
14   </servlet>
15   <servlet>
16     <servlet-name>XlsServlet</servlet-name>
17     <servlet-class>net.sf.jasperreports.j2ee.servlets.XlsServlet</servlet-class>
18   </servlet>
19   <servlet>
20     <servlet-name>DocxServlet</servlet-name>
21     <servlet-class>net.sf.jasperreports.j2ee.servlets.DocxServlet</servlet-class>
22   </servlet>
23 
24   <servlet-mapping>
25     <servlet-name>PdfServlet</servlet-name>
26     <url-pattern>/servlets/pdf</url-pattern>
27   </servlet-mapping>
28   <servlet-mapping>
29     <servlet-name>ImageServlet</servlet-name>
30     <url-pattern>/servlets/image</url-pattern>
31   </servlet-mapping>
32   <servlet-mapping>
33     <servlet-name>XlsServlet</servlet-name>
34     <url-pattern>/servlets/xls</url-pattern>
35   </servlet-mapping>
36   <servlet-mapping>
37     <servlet-name>DocxServlet</servlet-name>
38     <url-pattern>/servlets/docx</url-pattern>
39   </servlet-mapping>
40 
41 </web-app>

View Code

  7. report.js

葡京网上娱乐场 13葡京网上娱乐场 14

/***
 * 参数说明:{jasper:"report01.jasper",args:arr}
 * param.jasper:jasper文件名称,最好不要用中文
 * param.ds:dataSource文件名称,最好不要用中文
 * param.args:*格式待定*,暂时用数组,以后看使用什么格式方便可以再改
 * author: suxy.
 */
function openReport(win, param){
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    var url = postPath;

       if(!param.jasper){
           alert("jasper参数为空!");
           return false;
       }

    //弹出报表展示窗口,可选择更多的功能,不只是打印,还有导出excel等等
    url+="/report/common/showReport.jsp?jasper="+param.jasper+"&ds="+param.ds+"&args="+param.args;
    url=encodeURI(url);
    //打开新窗口
//    var win=window.open("about:blank");
    win.document.write("报表加载中,请稍候..");
    //创建form对象
    var win_form=win.document.createElement("form");
    win_form.action=url;
    win_form.method="POST";
    win.document.body.appendChild(win_form);
    //参数设置(创建input对象)
    var win_form_input=win.document.createElement("input");
    win_form_input.type="hidden";
    win_form_input.name='fileName';
    win_form_input.value=param.fileName;
    win_form.appendChild(win_form_input);
    //提交表单
    win_form.submit();
}

View Code

 

七. 功用亮

  访问:http://localhost:8080/ims/mms/receive.do,点击 打印 按钮

葡京网上娱乐场 15

  右上斗的按钮可以兑现打印、pdf导出、word导出