Jump to content

Recommended Posts

im trying to make a script which pulls data from a file and im trying to format it

 

and example string would be

20-11-2009, 14:21:00:: Unknown error type: [8] Undefined index: SESSION_ERROR on /home/me/public_html/error.php line 19

and i want to change it to

<b>20-11-2009, 14:21:00::</b> Unknown error type: [8] Undefined index: SESSION_ERROR on /home/me/public_html/error.php line 19

 

the code i have is

$data = preg_replace('/(0-9){2}-(0-9){2}-(0-9){4} (0-9){2}:(0-9){2}:(0-9){2}::/', '<b>\1-\2-\3 \4:\5:\6::</b>', $data);

but it doesnt work anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/184779-match-a-date/
Share on other sites

To expand on why your original code doesn't work Renlok. You encase the 0-9 with normal brackets, this creates a capture group, the problem is your capture group looks for the literal 0 followed by dash followed by 9 because you have not made it a character class by also encasing them in square brackets. ([0-9]{2}) would have been the correct syntax to match 2 single digits. Another reason your pattern doesn't match is because you don't include the comma in your pattern, which is present in the input. As JAY6390 has pointed out, since you are basically just encasing the whole thing in a pair of tags, you might as well capture it all as one. Also bare in mind that \d is not strictly speaking always the same as [0-9].

Link to comment
https://forums.phpfreaks.com/topic/184779-match-a-date/#findComment-975476
Share on other sites

If it's always there and always in the same format you could always use substr or similar to add in the tags, there's a fair chance it would be quicker. Point is there's dozens of different methods to use and probably 100s of different Regex patterns. For example you could probably do something along the lines of...

 

'#^(.{22})#'

 

But since the OP says the data is being pulled from another file I'd imagine it's coming from the middle of a string.

Link to comment
https://forums.phpfreaks.com/topic/184779-match-a-date/#findComment-975564
Share on other sites

Use this

$result = preg_replace('/(\d{2}-\d{2}-\d{4}, \d{2}:\d{2}:\d{2}::)/s', '<b>$1</b>', $subject);

 

Just note that you don't really need that capture, as the entire pattern is already represented by $0. Also, you are using the s modifier, but is not necessary as your pattern doesn't rely on a dot_match_all (.) with the need to include newlines.

 

As cags mentions, there are numerous ways to accomplish tasks.. here is yet another alternative:

 

$result = preg_replace('#(?:\d{2}-){2}\d{4}, (?:\d{2}{3}:#', '<b>$0</b>', $subject);

Link to comment
https://forums.phpfreaks.com/topic/184779-match-a-date/#findComment-976348
Share on other sites

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.