#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Preg_replace question


    Hello,

    I have a field named 'startdatetime' in the following format: "%m-%d-%Y %H:%M:%S"

    I would like to change the font color of %H:%M:%S" to red.

    Is this possible?

    thanks

    lhardee
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Yes.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0
    LOL!!

    I kinda figured it was, that was my lead-in for assistance
  6. #4
  7. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0
    Thanks for the link.

    I'll try my question another way.

    After some research about preg_replace, I think I'm on the right track. I would like to change the color of %H:%M:%S to red. I know the code below is incorrect. If you or anyone has suggestions of how to accomplish what I am trying to do, it would be appreciated.

    Code:
    $this->startdatetime->ViewValue = preg_replace('/^([0-5][0-9]):([0-5][0-9]):([0-5][0-9])?$/', '<span style=\"color:red">$2</span>', $this->startdatetime->ViewValue);
    Thanks,
    lhardee

    Comments on this post

    • ptr2void agrees : There you go, you're learning how to ask a question on a forum!
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Technically speaking the answer (yes/no) depends on your output medium. If you are writing to a text file, the answer is no. If you are outputting HTML, the answer is yes; you would use HTML and CSS to do it.

    I see you're using HTML, so the answer is yes.

    The ^ at the start of your regular expression tells it to match only at the beginning of the string. Since your string also contains the date, this seems to be incorrect.

    The ? near the end of your regular expression makes the last subgroup optional, which doesn't really seem to make sense. If you wanted to make the seconds part optional, you would probably want to enclose the last : inside a subgroup also. You can nest subgroups. Subgroups are the things between parenthesis. If none of it is supposed to be optional then remove the ?.

    For some reason your span tag has a random backslash in the middle of it.

    The $2 in your replacement string substitutes in the 2nd subgroup, which in this case would be the minutes value. You probably want to substitute the entire match, which would be $0.
    Last edited by E-Oreo; July 19th, 2012 at 10:45 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0
    Thank you so much!

    Still not working. However your instructions explained a lot that I did not understand. I removed the ? because it is not optional as well as other issues you pointed out. I feel as though I'm getting closer. Again...thanks.

    Code:
    $this->startdatetime->ViewValue = preg_replace('/([0-5][0-9]):([0-5][0-9]):([0-5][0-9])/', '<span style="color:red">$0</span>', $this->startdatetime->ViewValue);
    lhardee
  14. #8
  15. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    You could also consider forgetting about regex and just doing

    php Code:
    $split = explode(' ', $this->startdatetime->ViewValue);
    $this->startdatetime->ViewValue = sprintf("%s <span style=\"color:red\">%s</span>", $split[0], $split[1]);
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0
    That is awesome!!!

    I'm not familiar with $split = explode but will be reading up on it as well as preg_replace.

    That did the trick.

    I appreciate everyone's guidance.



    lhardee
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Click the word "explode" in that code, you'll be taken to the manual page for that function.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo