EDIT: I am trying to match the year in the string below
$expression = "BORN ON:</th><td>weekday, Month Day, Year
hr:min:sec AM timezone</td>"
AM
maybe pm
, timezone
could be pst
etc year is always 20-something
I've tried:
$pattern = "~BORN ON:</th><td>\w+(/20\d\d/)\w+</td>~";
to no avail, help is appreciated!
Thanks
you should backslash " /
" symbols. and make sure you have leading and trailing slash in your expression eg
$pattern = "~BORN ON:<\/th><td>\w+(\/20\d\d/)\w+<\/td>~";
preg_match("/...../", $where, $results);
secondly, here is updated expression:
preg_match("/BORN ON:<\/th><td>\w, \w [0-9]+, [0-9]+ [0-9]+:[0-9]+:[0-9]+ (AM|PM) \w <\/td>/i", $where, $results);
not tested, but should match your needs. also, enclose parts that you need to appear in results - atm, all date would be found in $results[1] if matched.
upd
preg_match("/BORN ON:<\/th><td>(\w, \w [0-9]+, ([0-9]+) [0-9]+:[0-9]+:[0-9]+ (AM|PM) \w) <\/td>/i", $where, $results);
$results[1] now would contain year
Try to get this string separately :
$string_time = "weekday, Month Day, Year hr:min:sec AM timezone" ;
// you use str_replace to get it
then use php builtin function :
$the_time = strtotime($string_time);
now you can manipulate it anyway you want, if that's what you are looking for.
You can use date("...",$string_time)
to get it in any format you want. ex:
echo date("Y-m-d",$string_time);
I matched using this in Ruby. Regex is prolly same in PHP. Try it out. Let me know if you have any issues. I would love to help. Good luck! :)
string = "BORN ON:</th><td>weekday, Month Day, Year hr:min:sec AM timezone</td>"
regex = /<\/th><td>([^,]+)\s*,\s*([^ ]+)\s+([^,]+)\s*,\s*([^ ]+)\s+([^:]+):([^:]+):([^ ]+)\s+(AM|PM)([^<]+)<\/td>/i
if regex.match(string)
puts "match"
puts "#{$1} is weekday"
puts "#{$2} is month"
puts "#{$3} is day"
puts "#{$4} is year"
puts "#{$5} is hour"
puts "#{$6} is minutes"
puts "#{$7} is seconds"
puts "#{$8} is Am or Pm"
puts "#{$9} is timezone"
else
puts "no match"
end
我得到这个工作:
"~BORN ON:</th><td>\w+, \w+ [0-9]+, ([0-9]+)~";
How about something like this to extract the year?
$strs = array(
"BORN ON:</th><td>weekday, Month 05, 2011 hr:min:sec AM timezone</td>",
"BORN ON:</th><td>Sunday, Jan 05, 2010 hr:min:sec AM timezone</td>",
"BORN ON:</th><td>Tuesday, 11 05, 2019 hr:min:sec AM timezone</td>"
);
foreach( $strs as $subject)
if( preg_match('%BORN ON:</th><td>\w+,\s*\w+\s*\w+,\s*(\d+)\s*%im', $subject, $regs))
echo $regs[1] . "\n";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.