Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/144373-format-date-in-preg_replace/
Share on other sites

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

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) ...
<?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);
?>

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?

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.