我需要在文本中的特定字符串后获取值。
即,
PHONE NUMBER 09878839934
CITY CHENNAI
EMAIL indhuk23@gmail.com
我需要使用php保存这些值。我打算这样做:
我匹配文本中的“PHONE NUMBER”字符串。将其后的值添加到“CITY”。 然后匹配“CITY”..值到“EMAIL”。 并且最后一个值。
此外,每个电子邮件ID的格式都会更改。 所以我实际上从具有基于电子邮件格式的数据库中获取格式。
你可以帮帮我吗?修改的
该文本是收到我的收件箱的电子邮件。因为我收到每封电子邮件,我需要检查“来自”id。基于格式不同。因此,收到电子邮件需要检查数据库中的“来自id”。我得到数据库的内容,关于邮件的格式。基于此我需要从邮件中检索值。
修改
例如:
我的表格如下:
email field1 field2 field3 field4 field5
***.com Name City Service Phone Number Preference
***.com Customer Name Place Looking for Mobile service Preference
答案 0 :(得分:1)
您也可以使用命名捕获gropus,如下所示:
^PHONE\sNUMBER\s+(?P<phone>.+)\R
CITY\s(?P<city>.+)\R
EMAIL\s(?P<email>.+)
在PHP
中,这将是:
$regex = '~
^PHONE\sNUMBER\s+(?P<phone>.+)\R
CITY\s(?P<city>.+)\R
EMAIL\s(?P<email>.+)~mx';
preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
// now you can loop over the matches
foreach ($matches as $match) {
echo $match["city"]; // etc.
}
参见 demo on regex101.com 。
答案 1 :(得分:0)
假设你的输入文字与你的例子(空格和新行)一样,你需要这样的东西:
$text = "PHONE NUMBER 09878839934
CITY CHENNAI
EMAIL indhuk23@gmail.com";
$pattern = "/PHONE NUMBER (.*)\RCITY (.*)\REMAIL (.*)/";
preg_match($pattern, $text, $matches);
var_dump($matches); // indexes 1,2 and 3 are phone number, city and email
答案 2 :(得分:0)
<?php
$username="root";$password="";$database="format";
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$ead="quikr.com";
$query=mysql_query("select * from mail where email ='$ead'");
$row = mysql_fetch_array($query);
$f1 = $row['field1'];
$f2 = $row['field2'];
$f3 = $row['field3'];
$f4 = $row['field4'];
$f5 = $row['field5'];
$f6 = $row['field6'];
$input="Name\n\nSindhuj City :Chennai- Service Pest ID mail.com Phone Number 9043234342 Preference redident";
$input=preg_replace('/\R/','', $input);
$input=preg_replace('/[^a-z0-9]/i', ' ', $input);
//$fone='/\b['.$f1.','.$f2.','.$f3.','.$f4.','.$f5.','.$f6.']+\b/i';
$fone='/'.$f1.'(.*?)'.$f2.'/';
preg_match($fone,$input, $display);
print_r($display[1]);
$ftwo='/'.$f2.'(.*?)'.$f3.'/';
preg_match($ftwo,$input, $display);
print_r($display[1]);
$ftwo='/'.$f3.'(.*?)'.$f4.'/';
preg_match($ftwo,$input, $display);
print_r($display[1]);
$ftwo='/'.$f4.'(.*?)'.$f5.'/';
preg_match($ftwo,$input, $display);
print_r($display[1]);
$ftwo='/'.$f5.'(.*?)'.$f6.'/';
preg_match($ftwo,$input, $display);
print_r($display[1]);
?>