MicBehrens
MicBehrens

Reputation: 1798

Get text contained within the parentheses at the end of string

Say I have this user list:

Michael (43)
Peter (1) (143)
Peter (2) (144)
Daniel (12)

The number in the furthest right set of parentheses is the user number.

I want to loop each user and get the highest user number in the list, which in this case would be 144. How do I do this? I'm sure it can be done with some kind of regexp, but I have no idea how. My loop is simple:

$currentUserNO = 0;

foreach ($users as $user) {
    $userNO = $user->NameUserNo; // NameUserNo is the string to be stripped! ex: "Peter (2) (144)" => 144

    if ($userNO > $currentUserNO) {
        $currentUserNO = $userNO;
    }
}

echo "The next user will be added with the user number: " . $currentUserNO + 1;

Upvotes: 3

Views: 1344

Answers (6)

Eineki
Eineki

Reputation: 14909

If you are not confortable with regular expression you should not use them (and start to seriously learn them* as they are very powerful but cryptic).

In the mean time you don't have to use regex to solve your problem, just use (assuming that the NameUserNo contains just a line of the list) :

$userNO = substr(end(explode('(',$user->NameUserNo;)),0,-1);

It should be easier to understand.

* Is there a good, online, interactive regex tutorial?

Upvotes: 1

Jacob Lambert
Jacob Lambert

Reputation: 7679

I'm fairly new to PHP, but couldn't you do it with:

$exploded = explode(" ", $user->NameUserNumber);
$userNo = substr(end($exploded), 1,-1);

Upvotes: 0

i alarmed alien
i alarmed alien

Reputation: 9520

This is pretty easy to do with a regex.

foreach ($users as $user) {
    # search for any characters then a number in brackets
    # the match will be in $matches[1]
    preg_match("/.+\((\d+)\)/", $user->NameUserNo, $matches);
    $userNO = $matches[1];
    if ($userNO > $currentUserNO) {
        $currentUserNO = $userNO;
    }
}

Because regexs use greedy matching, the .+, which means search for one or more characters, will grab up as much of the input string as it can before it reaches the number in brackets.

Upvotes: 0

jeroen
jeroen

Reputation: 91734

You could use a regex like:

/\((\d+)\)$/
          ^ glued to the end of the string
        ^^ closing parentheses
    ^^^ the number you want to capture
 ^^ opening parentheses

to capture the number in the last set of parentheses / at the end of the string.

But you could also use some basic array and string functions:

$parts = explode('(', trim($user->NameUserNo, ' )'));
$number = end($parts);

which breaks down to:

  • trim the closing parentheses and spaces from the end (strictly speaking from the beginning and end, you could also use rtrim());
  • explode on the opening parentheses;
  • get the last element of the resulting array.

Upvotes: 3

arkascha
arkascha

Reputation: 42915

I guess this is basically what you are looking for:

<?php
$list = array();
foreach $users as $user) {
  preg_match('/$([a-zA-Z]+).*\([1-9]+\)$/', , $tokens);
  $list[$tokens[2]] = $tokens[1];
}
ksort($list);
$highest = last(array_keys($list));

echo "The next user will be added with the user number: " . $highest++;

Upvotes: 0

stonewareslord
stonewareslord

Reputation: 353

I think the regular expression you are looking for is:

.+\((\d+)\)$

Which should select all characters until it reaches the last number wrapped in parenthesis.

The PHP code you can use to extract just the number is then:

$userNO = preg_replace('/.+\((\d+)\)$/', '$1', $user);

I haven't tested this, but it should set $userNO to 43 for the user Michael and 143 for the user Peter and so on.

Upvotes: 0

Related Questions