• 19.03.2026, 04:39
  • Register
  • Login
  • You are not logged in.

 

Dear visitor, welcome to Aqua Computer Forum. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Dominik

Newbie

[PHP]Letzte Zahl aus einem String extrahieren

Saturday, June 19th 2004, 6:22pm

Hallo,

ich habe hier zwei strings, aus beiden möchte ich mit einer Funktion jeweils die letzte Zahl herausfiltern.
Hier mal die zeichenketten:

a:2:{s:11:"autologinid";s:32:"37f8bf33853e0a8c55c5fd2a26f9ffc0";s:6:"userid";i:3;}
a:2:{s:11:"autologinid";s:32:"913dca9aae0fcd681548656c7ed7e7e7";s:6:"userid";s:1:"342";}

Sprich ch möchte die 3 und die 342 extrahieren.
Es gibt ja in PHP eine passende Funktion dazu: strrchr
aber wie mach ich das jetzt mit einer variablen zahl?

Gruß Dominik

Y0Gi

God

Re: [PHP]Letzte Zahl aus einem String extrahieren

Saturday, June 19th 2004, 7:30pm

ich bastel dir grad mal was...

edit: da:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<html>
<body>
<pre>
<?php
$pattern = '/^(.*)\b(\d+)\b(.*)$/';
$subjects = array(
    'a:2:{s:11:"autologinid";s:32:"37f8bf33853e0a8c55c5fd2a26f9ffc0";s:6:"use rid";i:3;}',
    'a:2:{s:11:"autologinid";s:32:"913dca9aae0fcd681548656c7ed7e7e7";s:6:"use rid";s:1:"342";}'
);

foreach ($subjects as $subject) {
    echo "Durchsuche String: $subject\n";
    if (preg_match($pattern, $subject, $matches)) {
        echo "Zahl gefunden: {$matches[2]}\n";
    } else {
        echo "Keine Zahl gefunden :´(\n";
    }
    echo "\n";
    // print_r($matches); // einkommentieren für detailansicht der matches
}
?>
</pre>
</body>
</html>

viel spaß.

Dominik

Newbie

Re: [PHP]Letzte Zahl aus einem String extrahieren

Saturday, June 19th 2004, 10:14pm

Super, klasse. vielen dank