19

$open_email_msg を検索するスクリプトを取得したいのですが、電子メールによって情報は異なりますが、以下のように同じ形式になります。

私は正規表現をあまり使用していませんが、私がやりたいのは、「タイトル:[タイトルのデータ]」、「カテゴリ:[カテゴリのデータ]」を検索する文字列を検索することです。みたいなことは考えないから

strpos($open_email_msg, "Title: (*^)"); 

うまくいくでしょう。

これはコード全体のスニペットにすぎません。残りは情報を MySQL テーブルに挿入し、サイトのニュース記事に投稿します。

誰かがこれに対する解決策を見つけるのを手伝ってくれますか?

厳密な電子メール メッセージ形式:

ニュース更新の
タイトル: 記事のタイトル
タグ: tag1 tag2
カテゴリ: 記事のカテゴリ、2 番目の記事のカテゴリ
スニペット: 記事のスニペット。
メッセージ: 記事のメッセージ。画像。より多くのテキスト、より多くのテキスト。Lorem impsum dolor sit amet.

<?php
    //These functions searches the open e-mail for the the prefix defining strings.
        //Need a function to search after the space after the strings because the subject, categories, snippet, tags and message are constant-changing.
    $subject = strpos($open_email_msg, "Title:");       //Searches the open e-mail for the string "Title" 
        $subject = str_replace("Title: ", "" ,$subject);
    $categories = strpos($open_email_msg, "Categories:");       //Searches the open e-mail for the string "Categories"
    $snippet = strpos($open_email_msg,"Snippet");           //Searches the open e-mail for the string "Snippet"
    $content = strpos($open_email_msg, "Message");  //Searches the open-email for the string "Message"
    $tags = str_replace(' ',',',$subject); //DDIE
    $uri =  str_replace(' ','-',$subject); //DDIE
    $when = strtotime("now");   //date article was posted
?>
4

2 に答える 2

29

PREG_OFFSET_CAPTUREのフラグを使用してみてくださいpreg_match。このようなもの:

preg_match('/Title: .*/', $open_email_msg, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][1];

これにより、文字列の初期位置がわかります。

私が使用している正規表現は間違っている可能性があり、行末などを考慮していないことに注意してください。ただし、それは別の問題です。:)

編集します。あなたが望むもののためのより良い解決策(私がそれを正しく理解している場合)は次のようなものです:

$title = preg_match('/Title: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';

次に、タイトルを$title変数に取得し、タイトルが見つからなかった場合は空の文字列を取得します。

于 2011-12-22T21:19:39.760 に答える
8

正規表現の strpos の代わりに preg_match を使用できます

preg_match (regex, $string, $matches, PREG_OFFSET_CAPTURE);

PREG_OFFSET_CAPTURE gives you the position of match.
于 2011-12-22T21:27:21.937 に答える