By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,935 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,935 IT Pros & Developers. It's quick & easy.

Formating this string

P: n/a
I receive string like this:
"aaa.bbb.ccc.com <20000>"

I need to get only number 20000.
How can I get numbers betwwen the "<>"

Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
$pattern = "*\<[0-9]+\>*";
$in = "aaa.bbb.ccc.com <20000>";

preg_match($pattern, $in, $matches);
$out = trim($matches[0], "<>"); //$out now has '20000'

I'm not much of a regexp guy, so there might be a cleaner pattern.
Nonetheless, that should work.

Jul 17 '05 #2

P: n/a
ZeldorBlat wrote:
$pattern = "*\<[0-9]+\>*";
$in = "aaa.bbb.ccc.com <20000>";

preg_match($pattern, $in, $matches);
$out = trim($matches[0], "<>"); //$out now has '20000'

I'm not much of a regexp guy, so there might be a cleaner pattern.
Nonetheless, that should work.

Thank You
Jul 17 '05 #3

P: n/a
ZeldorBlat wrote:
$pattern = "*\<[0-9]+\>*";
$in = "aaa.bbb.ccc.com <20000>";

preg_match($pattern, $in, $matches);
$out = trim($matches[0], "<>"); //$out now has '20000'

I'm not much of a regexp guy, so there might be a cleaner pattern.
Nonetheless, that should work.


$in = "aaa.bbb.ccc.com <20000>";
$pattern = "/$.*\<(\d+)\>.*^/";
$out = preg_replace($pattern, "$1", $in);

I didn't test this, but I attempt to "drop everything from the beginning
of string up until '<' and after '>'".

--
Markku Uttula
Jul 17 '05 #4

P: n/a
while everyone's code works..
(actually, I'm not sure about the examples with \< and \> in the
expression... "<" and ">" need not be escaped. )

anyhow, here's my take:

$string = 'aaa.bbb.ccc.com <20000>';
if ( preg_match('/<(\d+)>/',$string,$matches) )
$num = $matches[1]; // $num in this example is now '20000'
else {
// no num inside <> found
}

Jul 17 '05 #5

P: n/a
Markku Uttula wrote:
ZeldorBlat wrote:
$pattern = "*\<[0-9]+\>*";
$in = "aaa.bbb.ccc.com <20000>";

preg_match($pattern, $in, $matches);
$out = trim($matches[0], "<>"); //$out now has '20000'

I'm not much of a regexp guy, so there might be a cleaner pattern.
Nonetheless, that should work.

$in = "aaa.bbb.ccc.com <20000>";
$pattern = "/$.*\<(\d+)\>.*^/";
$out = preg_replace($pattern, "$1", $in);

I didn't test this, but I attempt to "drop everything from the beginning
of string up until '<' and after '>'".

Ok, try and adhere to the KISS philosophy. Observe.

$in = 'aaa.bbb.ccc.com <20000>';

$in = preg_replace('/<([0-9]+)>/', '$1', $in);

$in will now contain '2000'
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.