PHP file_get_contents输出恼人的字符?

时间:2016-07-08 01:04:00

标签: file-get-contents

我试图从网站上找到file_get_contents,但它会输出奇怪的字符,例如 } r H y M n '2I “ ^ÏjI-[D T8w | -Y

<?
  echo file_get_contents("http://mp3yum.top");
?>

有没有办法从这个网站上删除一些内容。

1 个答案:

答案 0 :(得分:2)

在调用外部源之前,您需要使用CURL。

package com.trv.cbia.de.tika;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


public class DocParser {

public Map<String, Object> processRecord(String path) {
    Map<String, Object> map = new HashMap<String, Object>();
    String docPath = path;
    try{
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            InputStream inputstream = new FileInputStream(new File(docPath));
            ParseContext pcontext = new ParseContext();
            Parser pdfparser  = new AutoDetectParser();
            pdfparser.parse(inputstream, handler, metadata, pcontext);
            map.put("text", handler.toString().replaceAll("\n|\r|\t", " "));
             map.put("title", metadata.get(TikaCoreProperties.TITLE));
             map.put("pageCount", metadata.get("xmpTPg:NPages"));

    } catch (IOException ex){
         System.out.println("Caught IOException:" + ex.getMessage());
    }
    catch(TikaException tx) {
         System.out.println("Caught TikaException: " + tx.getMessage());
    }
    catch(SAXException sx){

         System.out.println("Caught SAXException: " + sx.getMessage());

        }

    return map;
}

 public static void main(String args[]){


    String file = args[0];
    String out =  args[1];
    DocParser textExtract = new DocParser();
    Map<String, Object> extractedMap = textExtract.processRecord(file);
    try {
        PrintWriter writer = new PrintWriter(out,"UTF-8");
        writer.println(extractedMap.get("text"));
        writer.flush();
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

}

}

输出屏幕:

enter image description here