Taking out numbers from a specific string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Taking out numbers from a specific string

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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];
} 
 
?>
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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
Post Reply