pianoman993 Posted February 8, 2009 Share Posted February 8, 2009 Hello there PHP experts. I have a small question for you. Is it possible to format a date (number) in a preg_replace statement? Here is a snippet of code I am using, you will see the unix_to_human($2) is my stab at a conversion to a real date. Is it possible to do this? If so, could please post an example? Thank you! '<table class="bbc_quote_table"><tr><td class="bbc_quote_header"><b>Quote from </b>$1</b> on unix_to_human($2)</td></tr><tr><td class="bbc_quote_body">', Any help is greatly appreciated - Pianoman993 Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/ Share on other sites More sharing options...
.josh Posted February 8, 2009 Share Posted February 8, 2009 Can't help you without 'before' and 'after' examples. But it sounds like you are just wanting to convert a unix timestamp to a date string. Look at the date function. Or if you already have a date string and want to make it look different, look at strtotime + date Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757588 Share on other sites More sharing options...
pianoman993 Posted February 8, 2009 Author Share Posted February 8, 2009 Sorry about that. Here's my php code function bb_encode ($str) { $str = nl2br(htmlentities($str)); $simple_search = array( '~\[quote author=(.*?) date=(.*?)\]~is', '~\[/quote\]~is', ); $simple_replace = array( '<table class="bbc_quote_table"><tr><td class="bbc_quote_header"><b>Quote from </b>$1</b> on unix_to_human($2)</td></tr><tr><td class="bbc_quote_body">', '</td></tr></table>' ); // Do simple BBCode's $str = preg_replace ($simple_search, $simple_replace, $str); return $str; } And here is what I right [ quote author=UrbanTwitch date=1227397761]Thank you so much!! [ /quote ] Here is what I get Quote from UrbanTwitch on unix_to_human(1227397761) ... Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757592 Share on other sites More sharing options...
pianoman993 Posted February 8, 2009 Author Share Posted February 8, 2009 Have I stumped you all? Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757651 Share on other sites More sharing options...
.josh Posted February 8, 2009 Share Posted February 8, 2009 <?php function bb_encode ($str) { $str = nl2br(htmlentities($str)); preg_match('~\[quote.*?date=(\d+?)\]~is',$str,$timestamp); $date = date("m:d:Y H:i:s", $timestamp[1]); $simple_search = array( '~\[quote author=(.*?)[^\]]*\]~is', '~\[/quote\]~is', ); $simple_replace = array( '<table class="bbc_quote_table"><tr><td class="bbc_quote_header"><b>Quote from </b>$1</b> on ' . $date .'</td></tr><tr><td class="bbc_quote_body">', '</td></tr></table>' ); // Do simple BBCode's $str = preg_replace ($simple_search, $simple_replace, $str); return $str; } $str = "[quote author=UrbanTwitch date=1227397761]Thank you so much!![/quote]"; echo bb_encode($str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757659 Share on other sites More sharing options...
pianoman993 Posted February 8, 2009 Author Share Posted February 8, 2009 Thank you for the response It worked! Just one last question, how could I convert that back to it's original number? Thanks for being so helpful! - Pianoman993 Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757719 Share on other sites More sharing options...
.josh Posted February 8, 2009 Share Posted February 8, 2009 $timestamp[1] has the original number in it. Or you can take the date and use strtotime Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757744 Share on other sites More sharing options...
pianoman993 Posted February 8, 2009 Author Share Posted February 8, 2009 So in a nutshell, if I used this code: function bb_decode ($str) { preg_match('~\[quote.*?date=(\d+?)\]~is',$str,$timestamp); $date = strtotime("m:d:Y H:i:s", $timestamp[1]); $bbcode = array( "/\<strong>(.*?)<\/strong>/is" => "[b]$1[/b]", "/\<em>(.*?)<\/em>/is" => "[i]$1[/i]", "/\<u>(.*?)<\/u>/is" => "[u]$1[/u]", "/\<s>(.*?)<\/s>/is" => "[s]$1[/s]", "/\<table class=\"bbc_quote_table\"><tr><td class=\"bbc_quote_header\"><\/td><\/tr><tr><td class=\"bbc_quote_body\">/is" => "[quote]$1", "/\<table class=\"bbc_quote_table\"><tr><td class=\"bbc_quote_header\"><span class=\"quote_from\">Quote from<\/span> <span class=\"author\">(.*?)<\/span> on <span class=\"date\">(.*?)<\/span><\/td><\/tr><tr><td class=\"bbc_quote_body\">/is" => "[quote author=$1 date=$date]$3", "/\<\/td><\/tr><\/table>/is" => "[/quote]", "/\<br \/>/is" => "" ); I would be able to convert the date= field to the original time string? Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757778 Share on other sites More sharing options...
pianoman993 Posted February 9, 2009 Author Share Posted February 9, 2009 That's strange, for some reason I'm getting a bunch of errors now :/ A PHP Error was encountered Severity: Notice Message: Undefined offset: 1 Line Number: 250 A PHP Error was encountered Severity: Warning Message: Cannot modify header information - headers already sent by Severity: Warning Message: Cannot modify header information - headers already sent by Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757808 Share on other sites More sharing options...
pianoman993 Posted February 9, 2009 Author Share Posted February 9, 2009 It seems that this topic is not very exciting. I'm going to start a new one. In the new one I will give 10 dollars to the first one to fix this problem. Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757818 Share on other sites More sharing options...
pianoman993 Posted February 9, 2009 Author Share Posted February 9, 2009 I just created a new topic but have asked the forum mods to delete it. Basically, I would like to pay you if you are able to help me since you have been the one all along helping me! If you would like my offer to pay you, my offer of $10 still stands. * When the message form is submitted and sent to the bb_encoder function, the preg_replace that detects all quote tag dates and converts them automatically will generate a "headers already sent error" and "undefined offset 1 error" if no quote tag with dates is included. (Basically if you just submitted the words "Hello World" in the message textbox, you would get those errors) * When the tags [ quote author=... date=... ] [ / quote ] is actually found and the preg_replace function converts the date to readable format, it converts nicely. However if there are more than one sets of quotes, the date conversion is rendered the same for each occurrence. * How can you reverse this process in the bb_decoder? Quote Link to comment https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/#findComment-757826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.