Page 1 of 1

Taking out numbers from a specific string

Posted: Wed Jan 03, 2007 12:59 am
by dude81
Hello,
Dont know whether this is the right place to post or not, but I post here.
I get a string in

Code: Select all

$text='<xyz:firstattrib><xyz:new>400,500</xyz:new></xyz:firstattrib> This is first message, say hello world';
I use a regexp saying

Code: Select all

if(preg_match("/(<.*>)+/Us", $text)){
	$text = preg_replace("/(<.*>)/Us", '',$text);
}
the new text comes to

Code: Select all

$text='400,500 This is first message, say hello world';
Now what function I need to use to get that 400,500 out of the $text;

But rather extracting from here, I would like to extract it from the regexp, is it possible, is there any function which can do that?

Thank You
Dude 81

Posted: Wed Jan 03, 2007 4:59 am
by Ollie Saunders

Code: Select all

$matches = array();
if(preg_match("/(<[^\d,]+([\d,]+)[^\d,]+>)+/Us", $text, $matches)){
    //$text = preg_replace("/(<.*>)/Us", '',$text);
    print_r($matches);
}
Like this?

Posted: Wed Jan 03, 2007 5:02 am
by bokehman

Code: Select all

<?php


$text='<xyz:firstattrib><xyz:new>400,500</xyz:new></xyz:firstattrib> This is first message, say hello world'; 

if(preg_match("/(?:(?:\d)+,?)+\d+/", $text, $match))
{
    echo $match[0];
} 
 
?>

Posted: Wed Jan 03, 2007 5:27 am
by dude81
Hi bokehman and ole,

Great Thanks for your help. I found bokehmans PHP script for mime MimeMailer code useful for solving my regular expression problems. Though Im not dealing with html headers but I needed such a format..

Thank You