I need to be able to split a string that contains email's From information. From the string I need to extract $NAME and $EMAIL or whatever is available.
The string can be in the following formats:
"Santa Clause" <santa#example.com>
Santa Clause <santa#example.com>
<santa#example.com>
preg_match('#(?:"(?<name>[^"]+)"|(?<name>.+))?<(?<email>.+)>#U', $string, $matches);
echo var_dump($matches);
preg_match('#(?:"(?<name>[^"]+)"|(?<name>.+))?<(?<email>[^>]+)>#U', $string, $matches);
echo var_dump($matches);
Try one of the above. The former will allow more valid emails, whereas the latter is faster.
$string_to_check = '"Santa Clause" <santa#npole.com>'
$matches = array();
preg_match('/?([^<"]*)"?\s*<(\S*)>/',$string_to_check,$matches);
$matches[1] //=> Santa Claus
$matches[2] //=> santa#npole.com
If the separator is always the same character (e.g. the semicolon):
$items = explode($separator, $from);
Otherwise, browse around in the preg_XXX functions for regex-based string splitting.
For the mail adress, have a look at http://php.net/manual/en/function.preg-match.php. This is a function that matches a string against a regular expression. Here's a short intro into how to use regular expressions with PHP.
If you want to match the name also, it will be some effort, so I suggest you first develop a regular expression that can extract an email address out of your string and then augment it to find the name also.
Found this and it works great!
$parts = preg_split('/[\'"<>]( *[\'"<>])*/', $text, -1, PREG_SPLIT_NO_EMPTY);
Related
I have no idea how to make a regex , that's why i am asking this question.I have one string like chirag patel <chiragxxx#gmail.com>
I have a regex to get email id from the string.
preg_match("/\<(.*)\>/", $data['From'], $matches);
$email = $matches[1];
How to get name from above string using regex?
my expected output is: chirag patel.
You can use the regex
.*(?=\<(.*)\>)
check the demo here. here is the php code for the following
$re = '/.*(?=\<(.*)\>)/';
$str = 'chirag patel <chiragxxx#gmail.com>';
preg_match($re, $str, $matches);
var_dump($matches[0]);
Use this in php
$data['From'] = "chirag patel <chiragxxx#gmail.com>";
preg_match("/.*(?=\<(.*)\>)/", $data['From'], $matches);
print_r($matches); // 0 : name, 1 : email
You add a capturing group for the name.
preg_match("/(.*)\<(.*)\>/", $data['From'], $matches);
$name = $matches[1];
$email = $matches[2];
You may use this regex to capture name and email address in 2 separate groups:
(\pL+[\pL\h.-]*?)\h*<([^>]+)>
RegEx Demo
RegEx Breakup:
(\pL+[\pL\h.-]*?) # group #1 that match 1+ name consisting Unicode letters, dots, hyphens, spaces
\h*: Match 0 or more whitespaces
<([^>]+)>: group #2 to capture email address between < and > characters
Code:
preg_match('~(\pL+(?:[\pL\h-]*\pL)?)\h*<([^>]+)>~u', $str, $matches);
// Print the entire match result
print_r($matches);
I know it has been answered but because it's in PHP, which supports named patterns, and because might look cool:
/(?<name>.*?) \<(?<email>.*?)\>/g
name and email will be keys in the $matches array.
I'm trying to parse a string into components. My solution works for full strings, but I want to be able to account for strings with potentially fewer components. For instance, I want to be able to match G02F 1/1335, G02F 1, G02F, etc. With preg_match, if not all the capturing groups match, the entire output is invalid.
$string = 'G02F 1/1335';
$string = strtoupper(preg_replace('/\s+/', '', $string));
preg_match('%^([A-H])([0-9]{1,2})([A-Z])([0-9]{1,4})/([0-9]{1,6})$%', $string, $parsed);
As #mario suggested in comment, make subpatterns optional with ?:
preg_match( '%^([A-H])(\d{1,2})([A-Z])\s*(\d{1,4})?/?(\d{1,6})?$%', $string, $parsed );
i need your help about how to parse the string. I have a string with structure below :
MALANG|TVhHMTAwMDBK MALANGBONG,GARUT|QkRPMjA3MTlK MALANGKE BARAT,MASAMBA|VVBHMjMzMDVK MALANGKE,MASAMBA|VVBHMjMzMDRK
I'm now confuse how to parse this string so that i can get a pattern like this :
MALANG|TVhHMTAwMDBK
MALANGBONG,GARUT|QkRPMjA3MTlK
MALANGKE BARAT,MASAMBA|VVBHMjMzMDVK
MALANGKE BARAT,MASAMBA|VVBHMjMzMDVK
The pattern output are City_Name|RandomCode
I have try to use explode by space, but the city name sometimes also contains a space. What function in php i could use to solve this problem?
Try this one out. It fits your example ok
$str = 'MALANG|TVhHMTAwMDBK MALANGBONG,GARUT|QkRPMjA3MTlK MALANGKE BARAT,MASAMBA|VVBHMjMzMDVK MALANGKE,MASAMBA|VVBHMjMzMDRK';
$pattern = '/(?<=^| )[A-Z, ]+?\|[A-Za-z0-9]+(?= |$)/';
if (preg_match_all($pattern, $str, $matches)) {
$parts = $matches[0];
}
You may need to tweak some of the character classes if say your city names contain anything other than capital letters, spaces and commas.
Example here - http://codepad.viper-7.com/6ujl3p
Alternatively, if the RandomCode parts are guaranteed to all be 12 characters long, preg_split may be a better fit, eg
$pattern = '/(?<=\|[A-Za-z0-9]{12}) /';
$parts = preg_split($pattern, $str);
Demo here - http://codepad.viper-7.com/Wd4Wmc
I am pretty new to the use of preg_match. Searched a lot for an answer before posting this question. Found a lot of posts to get data based on youtube ID etc. But nothing as per my needs. If its silly question, please forgive me.
I need to get the ID from a string with preg_match. the string is in the format
[#1234] Subject
How can I extract only "1234" from the string?
One solution is:
\[#(\d+)\]
This matches the left square bracket and pound sign [#, then captures one or more digits, then the closing right square bracket ].
You would use it like:
preg_match( '/\[#(\d+)\]/', '[#1234] Subject', $matches);
echo $matches[1]; // 1234
You can see it working in this demo.
You can try this:
preg_match('~(?<=\[#)\d+(?=])~', $txt, $match);
(?<=..) is a lookbehind (only a check)
(?=..) is a lookahead
Your regular expression:
preg_match('/^\[\#([0-9]+)\].+/i', $string, $array);
That's a way you could do it:
<?php
$subject = "[#1234] Subject";
$pattern = '/^\[\#([0-9]+)/';
preg_match($pattern, $subject, $matches);
echo $matches[1]; // 1234
?>
To get only the integer you can use subpatterns http://php.net/manual/en/regexp.reference.subpatterns.php
$string="[#1234] Subject";
$pattern="/\[#(?P<my_id>\d+)](.*?)/s";
preg_match($pattern,$string,$match);
echo $match['my_id'];
I'm looking for an way to parse a substring using PHP, and have come across preg_match however I can't seem to work out the rule that I need.
I am parsing a web page and need to grab a numeric value from the string, the string is like this
producturl.php?id=736375493?=tm
I need to be able to obtain this part of the string:
736375493
$matches = array();
preg_match('/id=([0-9]+)\?/', $url, $matches);
This is safe for if the format changes. slandau's answer won't work if you ever have any other numbers in the URL.
php.net/preg-match
<?php
$string = "producturl.php?id=736375493?=tm";
preg_match('~id=(\d+)~', $string, $m );
var_dump($m[1]); // $m[1] is your string
?>
$string = "producturl.php?id=736375493?=tm";
$number = preg_replace("/[^0-9]/", '', $string);
Unfortunately, you have a malformed url query string, so a regex technique is most appropriate. See what I mean.
There is no need for capture groups. Just match id= then forget those characters with \K, then isolate the following one or more digital characters.
Code (Demo)
$str = 'producturl.php?id=736375493?=tm';
echo preg_match('~id=\K\d+~', $str, $out) ? $out[0] : 'no match';
Output:
736375493
For completeness, there 8s another way to scan the formatted string and explicitly return an int-typed value. (Demo)
var_dump(
sscanf($str, '%*[^?]?id=%d')[0]
);
The %*[^?] means: greedily match one or more non-question mark characters, but do not capture the substring. The remainder of the format parameter matches the literal sequence ?id=, then greedily captures one or more numbers. The returned value will be cast as an integer because of the %d placeholder.