Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex in a string for strpos()

Tags:

regex

php

strpos

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
?>
like image 857
brinard sweeting Avatar asked Dec 22 '11 21:12

brinard sweeting


People also ask

How do I find a character in a string in regex?

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).

What is strpos ()? Give example?

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.

What is the strpos () function used for?

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.


2 Answers

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.

like image 53
cambraca Avatar answered Oct 11 '22 14:10

cambraca


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.
like image 33
Shraddha Avatar answered Oct 11 '22 15:10

Shraddha