English 中文(简体)
PHP 爆炸() 允许通用字符
原标题:Are universal characters allowed in PHP explode()
  • 时间:2012-05-27 14:29:04
  •  标签:
  • php
  • regex

我需要在"太阳,2012年5月27日, 早上6点25分"之前得到一切

我希望得到所有东西之前, "在xxxxxxx,xxxxxx,"

问题是五月、二十七和六月的时间长度各不相同。什么是这项工作的最佳工具。由于我缺乏对正方位的处理经验,我正试图使用爆炸(),但似乎它无法在这里完成这项工作。我的最佳选择是什么?

[编辑]

我最后用了一个综合的答案。

preg_match ("/(*) Ons+( SunSatFriFriThuWedTueM),s+( 1月__ 2月__ 3月__ 3月__ 4月__ 5月__ 6月_ 7月__ 8月__ 9月__ 10月__ 11月__ 12月)s+d?d,s+d{4}s+ats+d?d:dds+[AP]M,/i”, $to, $end);

最佳回答

像这样,我想:

/Ons+(Sun|Sat|Fri|Thu|Wed|Tue|Mon),s+(January|February|March|April|May|June|July|August|September|October|November|December)s+d?d,s+d{4}s+ats+d?d:dds+[AP]M,/i

[编辑]

根据评论:我增加了对大小写不敏感的支持( 通过在正则结尾处添加 > 修饰符) 。 我还将表达式中的空格更改为 , 以允许任何空白字符, 并添加了 , 以允许单词之间的多个空格 。

我还没有修改它来支持长日名称或短月名称, 因为指定月份名称的问题在长度上是可变的, 但是没有指定日期名称是可变的 。 但是, 如果需要的话, 添加这些变量应该微不足道 。

[编辑]

$to = "Let me know how this response looks..... On Sun, May 27, 2012 at 6:25 AM, Pr";
preg_match("/Ons+(Sun|Sat|Fri|Thu|Wed|Tue|Mon),s+(January|February|March|April|May|June|July|August|September|October|November|December)s+d?d,s+d{4}s+ats+d?d:dds+[AP]M,/i", $to, $end);

此代码对您在评论中给出的示例有效 。

希望这有帮助。

问题回答
preg_match( /(.*?) On w+, w+ d?d, d+ at d?d:d?d ww,/ ,  grab this text here On Sun, May 27, 2012 at 6:25 AM, , $matches);
echo $matches[1];
// echoes  grab this text here 

(*?) 符合开头的所有内容, w/code> 符合字母数字字符 1 或更多次, d 符合一个或两个数字

一个正则表达式会有效, 因为正则表达式是为此而设计的: 选择基于模式的数据 。 但是, 您可以在, (comma) 上爆炸, 然后再将前四个元素一起插入, 以形成您的句子 。 我怀疑在此情况下使用正则表达式会更快 。

Ultimately it s your preference: which is better readable and understandable by you. The main advantage regular expression would have in this particular case is hat they can extract specific values/patterns, so you could easily have them set aside the month for instance.

$dateString = "On Sun, May 27, 2012 at 6:25 AM, some other text here";

// using explode/implode
$result = explode( , ,$dateString);

print "we got: " . implode( , , array_slice($result,0,3)) . "
";

// using regular expression
$pattern = "/On [A-Z,a-z]{3}, [A-Z,a-z]{3} [0-9]+, [0-9]{4} at [0-9,:]+ (?:A|P)M/U";

preg_match($pattern,$dateString,$match);

print "We got: " . $match[0] . "
";

也请阅读 rel=“nofollow”>PHP手册,定期表达小节,加上首页http://www.grymoire.com/Unix/Regular.html" rel=“nofollow”>tument

就个人而言,我认为正方字的表达方式在视觉和性能上都可能超乎想象。 但是,要学习常规表达方式,它们有时会很有帮助。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...