Renlok Posted December 11, 2009 Share Posted December 11, 2009 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? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 11, 2009 Share Posted December 11, 2009 Use this $result = preg_replace('/(\d{2}-\d{2}-\d{4}, \d{2}:\d{2}:\d{2}::)/s', '<b>$1</b>', $subject); Note you should really be using <strong> tags rather than <b> tags Quote Link to comment Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 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]. Quote Link to comment Share on other sites More sharing options...
nozai Posted December 11, 2009 Share Posted December 11, 2009 If your date is always there at the beginning of the string, and ends with 2 colons, you could just: $result = preg_replace('/^(.*?::)/s', '<b>$1</b>', $subject); Quote Link to comment Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted December 13, 2009 Share Posted December 13, 2009 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); Quote Link to comment 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.