-1

次のようなテキストファイルがあります。

<http://dbpedia.org/resource/Autism> <http://www.w3.org/2000/01/rdf-schema#comment> "Autism is a disorder of neural development characterized by impaired social interaction and communication, and by restricted and repetitive behavior. The diagnostic criteria require that symptoms become apparent before a child is three years old. Autism affects information processing in the brain by altering how nerve cells and their synapses connect and organize; how this occurs is not well understood."@en .
<http://dbpedia.org/resource/Anarchism> <http://www.w3.org/2000/01/rdf-schema#comment> "Anarchism is generally defined as the political philosophy which holds the state to be undesirable, unnecessary, and harmful, or alternatively as opposing authority and hierarchical organization in the conduct of human relations. Proponents of anarchism, known as \"anarchists\", advocate stateless societies based on non-hierarchical voluntary associations. There are many types and traditions of anarchism, not all of which are mutually exclusive."@en .
<http://dbpedia.org/resource/Achilles> <http://www.w3.org/2000/01/rdf-schema#comment> "In Greek mythology, Achilles was a Greek hero of the Trojan War, the central character and the greatest warrior of Homer's Iliad. Plato named Achilles the most handsome of the heroes assembled against Troy. Later legends (beginning with a poem by Statius in the 1st century AD) state that Achilles was invulnerable in all of his body except for his heel. As he died because of a small wound on his heel, the term Achilles' heel has come to mean a person's principal weakness."@en .

コード(ここでは関係ありません)を使用して、各行の最初のURLにある記事の名前を抽出しています。次に、引用符で囲まれた説明の最初の文を抽出します。問題は、最初の文の文字列をテーブルに挿入しようとすると、挿入が失敗することです(エコーは正常に機能します)。説明なしでタイトルを挿入するだけで問題ありません。説明が挿入を失敗させる理由を誰かが知っていますか?

最初の文を取得するために使用しているコードは次のとおりです。

 $data = fgets($handle); //get line

 $data = str_replace("> ", "-!-", $data);

 dataArr = explode("-!-", $data);

 //Get last part of uri from 1st element in array
  $title = getLastPartOfUrl($dataArr[0]);   
  $desc=preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', escape(substr($dataArr[2],1)));

  $db->query("insert into mytable SET title = '".$title."', desc ='".$desc."'");

  function escape($str)
    {
            $search=array("\\","\0","\n","\r","\x1a","'",'"');
            $replace=array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
            return str_replace($search,$replace,$str);
    }

編集:私はurlencodeとaddslashesの両方を試しましたが、どちらの場合も$desc文字列を含めると挿入が失敗します。

4

1 に答える 1

4

あなたは逃げませんtitle

escape私もあなたの機能を信用しません。何$dbであるかはわかりませんが、PDO/mysqliで適切にパラメーター化されたクエリを使用する必要があります

編集:DESCMySQLの予約語です。クエリでは、(列名として使用する場合は)バッククォートで囲む必要があります。

于 2013-02-26T00:27:45.647 に答える