简体   繁体   English

如何防止“ <<”破坏字符串

[英]How do I prevent that the “<<” breaks the String

I tryed to build a FDF-File in php but i have problems with "<<". 我试图在php中建立FDF文件,但是“ <<”有问题。 It's a bit strange the "<<" everything was before the string is deleted. 删除字符串之前的所有内容均<<<有点奇怪。 I know the character is an operator but how can I use it to construct a string now? 我知道字符是一个运算符,但是现在如何使用它来构造一个字符串?

here is my code: 这是我的代码:

$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';

    $fields = "";

    foreach($dataContainer as $data)
    {
        if(array_key_exists($data['fieldname'], $this->fieldSet))
        {
            $field = '('.$data['fieldname'].')';
            $value = '('.$data['content'].')';

            $fields.='<</T'.$field.'/V'.$value.'>>';

            echo $fields." ".$data['fieldname']."<br />";
        }
    }

    $fdf.= $fields;
    $fdf.= '] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF';

and the output is: 输出为:

<
<>
<><>
<><><>
<><><><>

Has anyone a solution for this? 有人对此有解决方案吗?

Your PDF is fine, there is nothing wrong with << or anything else in the code. 您的PDF很好, <<或代码中的任何其他内容都没有问题。 The only problem is that you probably dump the generated PDF content directly and, because nobody tells it otherwise, the browser tries to interpret it as HTML. 唯一的问题是,您可能直接转储了生成的PDF内容,并且由于没有人告知,否则浏览器会尝试将其解释为HTML。

The solution is very simple: use function header() before outputting the PDF to tell the browser the correct type of the content you send: 解决方案非常简单:在输出PDF之前使用函数header()来告诉浏览器您发送的内容的正确类型:

header('Content-Type: application/pdf');
// ... Generate the PDF here
// ... and output it
echo($fdf);

That's all. 就这样。 Now, the browser knows the body of the response is not HTML and it won't try to display it, except if it knows how to read PDF (which the latest versions of Firefox and Chrome apparently do). 现在,浏览器知道响应的主体不是HTML,并且不会尝试显示它,除非它知道如何读取PDF(最新版本的Firefox和Chrome显然可以读取)。

Depending on the browser and its settings, it will either display the PDF content rendered (as it would appear on the paper on printing) or it will open the default PDF viewer installed on the computer (Adobe Acrobat Reader, fe) or will ask where to save it or it will ask what of the above to do. 根据浏览器及其设置的不同,它会显示渲染的PDF内容(与打印时在纸张上显示的一样),或者会打开计算机(Adobe Acrobat Reader,fe)上安装的默认PDF查看器,或者询问在哪里保存它,否则它将询问以上操作。

Adding htmlentities() around strings with <, > characters should fix your problem: 在带有<, >字符的字符串周围添加htmlentities()可以解决您的问题:

$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';

    $fields = "";

    foreach($dataContainer as $data)
    {
        if(array_key_exists($data['fieldname'], $this->fieldSet))
        {
            $field = '('.$data['fieldname'].')';
            $value = '('.$data['content'].')';

            $fields.='<</T'.$field.'/V'.$value.'>>';

            echo $fields." ".$data['fieldname']."<br />";
        }
    }

    $fdf.= $fields;
    $fdf.= '] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF';

$fdf = htmlentities( $fdf ); // Adding it here would make sure every <, > is replaced

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM