I want to get the scripts to search the $open_email_msg which different e-mails will have different info but the same format as below.
I haven't really used regex much, but what i want to do is whenever i have it to search the string it would search for "Title: [data for title]", "Categories: [data for categories]. I am asking because i don't think something like
strpos($open_email_msg, "Title: (*^)");
would even work.
This is just a snippet of the whole code, the rest inserts the info into a MySQL table and then is posted to a News Article on the site.
Can somebody help me find a solution to this please?
Strict e-mail message format:
News Update
Title: Article Title
Tags: tag1 tag2
Categories: Article Category, 2nd Article Category
Snippet: Article snippet.
Message: Article Message. Images. More text, more text. 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
?>
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive. Note: This function is binary-safe.
strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.
Try using the PREG_OFFSET_CAPTURE
flag for preg_match
. Something like this:
preg_match('/Title: .*/', $open_email_msg, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][1];
This should give you the initial position of the string.
Note that the regex I'm using could be wrong and not take into account line endings and stuff, but that's another subject. :)
EDIT. A better solution for what you want (if I understand it correctly) would be something like this:
$title = preg_match('/Title: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';
You'd then get the title into the $title
variable, and an empty string if no title was found.
You can use preg_match instead of strpos for regex
preg_match (regex, $string, $matches, PREG_OFFSET_CAPTURE);
PREG_OFFSET_CAPTURE gives you the position of match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With