1

This is my first time using the trim function and I want to get the Time excluding the open and close parenthesis of my string. Can you give me hints and suggestions on how to do this?

$string = "Updated by Carewina Almonte  (04/02/2018 21:58:32)";     
echo trim($string, "Updated by Carewina Almonte  (04/02/2018");     
exit();
Virgil Joseph Cruz
  • 77
  • 2
  • 4
  • 12
  • https://stackoverflow.com/questions/11249445/php-regex-get-a-string-within-parentheses – Kevin Apr 17 '18 at 03:27
  • Possible duplicate of [PHP/REGEX: Get a string within parentheses](https://stackoverflow.com/questions/11249445/php-regex-get-a-string-within-parentheses) – Goma Apr 17 '18 at 03:29
  • 1
    if the format is constant, you could just use substr() other wise a regular expression, trim() is not really sutiable here –  Apr 17 '18 at 03:31

4 Answers4

3

In php, You can do get this and this works if and only if date and time always appears at the end of string -

 $string = "Updated by Carewina Almonte  (04/02/2018 21:58:32)";
 $time = substr($string,-9,8);
 echo $time;
Veshraj Joshi
  • 3,544
  • 3
  • 27
  • 45
  • I don't get it, why is this answer getting upvoted. OP has not mentioned that the string is of specific pattern. I think using regular expression is the only choice since we don't know anything except that the string is inside parentheses. –  Apr 17 '18 at 04:01
  • @roundAbout I posted this because there is a chance of a string pattern. If there exists pattern then this answer could be the simplest and effective one. I might be wrong because I always thinks that if something is going to be done with simple things then why to go to harder. Anyway thanks. You can downvote as you did. Someone might think it's better to go with simple solutions .... I think simple code also deserve upvote to... – Veshraj Joshi Apr 17 '18 at 05:15
1

You can use preg_match for this task:

$str = 'Updated by Carewina Almonte  (04/02/2018 21:58:32)';
preg_match('/(?<=\(\d{2}\/\d{2}\/\d{4} ).*(?=\))/', $str, $match);
echo $match[0];

Breakdown:

  • Positive Lookbehind (?<=\(). Assert that the Regex below matches:

    • \( matches the character ( literally (case sensitive)
    • .* matches any character (except for line terminators)
    • * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    • \d{2} matches a digit (equal to [0-9])
    • {2} Quantifier — Matches exactly 2 times
    • \/ matches the character / literally (case sensitive)
  • Positive Lookahead (?=\)). Assert that the Regex below matches:

    • \) matches the character ) literally (case sensitive)
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
  • 1
    he wants the time not the date. – Wils Apr 17 '18 at 03:35
  • @Wils I realized it after your comment. I have already updated the answer. – Aniket Sahrawat Apr 17 '18 at 04:13
  • One of the beautiful things `PHP` offers is the possibility to use other delimiters, so that you don't have to escape everything. That being said, you could rewrite your expression to `~(?<=\(\d{2}/\d{2}/\d{4} ).*(?=\))~`. – Jan Apr 17 '18 at 06:51
1

$string = "Updated by Carewina Almonte  (04/02/2018 21:58:32)";     

if(preg_match("/\d{2}:\d{2}:\d{2}/", $string , $match))
{

    echo $match[0];
}

Regular expression should be used in this case.

Wils
  • 1,178
  • 8
  • 24
1

For you example string you might match what is between parenthesis using \(\K[^)]+(?=\)).

This will match an opening parenthesis \( and then use \K to reset the starting point of the reported match.

After that match NOT a closing parenthesis one or more times [^)]+ and a positive lookahead to assert that what follows is a closing parenthesis (?=\)).

Then you could create a DateTime using or use DateTime::createFromFormat using $matches[0] and extract the time:

$re = '/\(\K[^)]+(?=\))/';
$str = 'Updated by Carewina Almonte  (04/02/2018 21:58:32)';
preg_match($re, $str, $matches);
$dateTime = new DateTime($matches[0]);
if ($dateTime !== false) {
    echo $dateTime->format('H:i:s');
}

Test

The fourth bird
  • 154,723
  • 16
  • 55
  • 70