i have random mobile number with text messages, how would i go about only viewing the numbers i want for example 1234

i have

foreach($message as $msg)

echo $msg;

which displays all the messages

Member Avatar
diafol

Show your data structure. Your $message array should hold a few items of data, e.g.

$messages[0]['id'],$messages[0]['msg'],$messages[0]['time']
$messages[1]['id'],$messages[1]['msg'],$messages[1]['time']

etc.

For example you can set a range from 1 to 4 and do a for loop until you complete the given range:

$a = range(1,4); #or start from zero
for($i = 1; $i < $a ; $i++) { echo $msg[$i]; }

im working with a soap service it just returns strings i need to sort out the strings some how

Member Avatar
diafol

OK, so show us the structure. Is the mobile number part of the messsage?

yeah the mobile number is part of the message also the date and time it appears in one string so i need someway of sorting it out

$client = new getsoap; 

$message = getmsg->peekMessage() ;

foreach($message as $msg){

echo $msg . '<p>';
}

location of the wsdl file
peekMessage is the fucntion used to get the messages from the soap service
https://m2mconnect.orange.co.uk/orange-soap/services/MessageServiceByCountry?wsdl

here is the output

44788844544544781781414920/02/2012 17:02:00SMS0Hello, how are you?

Member Avatar
diafol
$str = '44788844544544781781414920/02/2012 17:02:00SMS0Hello, how are you?';
preg_match('/^(\d{24})(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS\d(.*)/',$str,$matches);
print_r($matches);

preg_match?

how would i filter it out before i echo it out ?

Member Avatar
diafol

You retrieve the data as an array I take it?
So, for each peekMessage - if an array, e.g.:

$phone_number_to_match = '174387549272568';
foreach($peekMessage as $p){
  preg_match('/^(\d{24})(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS\d(.*)/',$str,$matches);
  if($matches[0] == $phone_number_to_match)echo $matches[4] . "<br />";
}

yeah it is an array thanks for your help ardav much appreciated

just one more thing what is $str variable in the preg_match function

Member Avatar
diafol

SOrry - should be $p

$phone_number_to_match = '174387549272568';
foreach($peekMessage as $p){
  preg_match('/^(\d{24})(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS\d(.*)/',$p,$matches);
  if($matches[0] == $phone_number_to_match)echo $matches[4] . "<br />";
}
Member Avatar
diafol
$phone_number_to_match = '174387549272568';
foreach($peekMessage as $p){
 // preg_match('/^(\d{24})(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS\d(.*)/',$p,$matches);
 // if($matches[0] == $phone_number_to_match)echo $matches[4] . "<br />";
  echo $p . "<br />";
}

do you get anything?

with them 2 lines comment out i get the block of message and phone numbers

Member Avatar
diafol

ok well it looks like the pattern is wrong. the first bit looks for a 24 digit number - if this number varies the search pattern will fail

could you give an example of a few messages so that we can assess the right pattern to apply?

In the meantime, how about:

$p = '44788844544544781781414920/02/2012 17:02:00SMS0Hello, how are you?'; //use the loop for $p
preg_match('/^(\d+)(\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2})SMS\d(.*)/',$p,$matches);
echo "Phone: " . $matches[1] . "<br />";
echo "Date/Time: " . $matches[2] . "<br />";
echo "Message: <br />" . $matches[3];

people send messages and remove messages from the server here is whats currently appears:

44797672147044781781414929/02/2012 13:38:14SMS0 ATE0 AT+CMGS=+447817814149 what about this time?

its seems like the all the messages have the same format, messages start after "SMS0"

Member Avatar
diafol

OK, this is one message - are all messages in the exact same format?
So I assume you want this sort of thing:

The string 44797672147044781781414929/02/2012 13:38:14SMS0 ATE0 AT+CMGS=+447817814149 what about this time?

Into

Number (id or something??): 447976721470447817814149
Date: 29/02/2012
Time: 13:38:14
(SMS0) - don't really need to capture this
Message: ATE0 AT+CMGS=+447817814149 what about this time?

If that's all you need, I think this will suffice:

preg_match('/^(\d+)(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS0(.*)/',$p,$matches);

However, if you need to extract further data from this??

yeah all the messages are in the same format?, i think that did the trick. thanks, so what does the (\d+) do in preg_match just trying to fully understand it so i can know where the right place to echo out the phone number, the message and the date

Member Avatar
diafol

The (\d+) captures the first set of numbers before the date/time. It means the 'group of at least one digits'.

so if i want to echo just the number i would put

echo "number:" . $p[1];
Member Avatar
diafol

No $matches[1] if you're using

preg_match('/^(\d+)(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS0(.*)/',$p,[B]$matches[/B]);

preg_match takes a number of params:
First is the pattern search/capture (the regex)
Second is the string to search ($p)
Third is the array that captures the matches ($matches) - for multiple matches, you start on $matches[1] NOT $matches[0]

BTW - I'm pretty slow with regard to regex, so if somebody else wants to butt in here with a different example or better explanation, feel free.

so how does the $matches part work?, because i get a Undefined offset: 1 error when i use:

echo "number: " . $matches[1];
Member Avatar
diafol

Oops. It looks as though the matches weren't found so you get an error - the array item doesn't exist.

If you do a direct reference:

$p = '44797672147044781781414929/02/2012 13:38:14SMS0 ATE0 AT+CMGS=+447817814149 what about this time?';

preg_match('/^(\d+)(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS0(.*)/',$p,$matches);
print_r($matches);

It works for me.

when i do print_r($matches); i just get array() array() array() array(), guessing they are just empty arrays, which is weird when i echo out $p i get all the messages when i try to echo $matches i get errors

Member Avatar
diafol

can you paste the full code and the full output of all the echoes (tghe ones that work)

here is how it works but its not in oo style

$username	= '';
$password = '';
$wsdl	 = 'https://m2mconnect.orange.co.uk/orange-soap/services/MessageServiceByCountry?wsdl';

$client = new SoapClient($wsdl);
$request = $client->peekMessages($username, $password, '10');
$count = count($request);

foreach($request as $msg){
echo $msg . '<br/>';
}

output :

44797672146744781781414929/02/2012 16:43:30SMS0 ATE0 AT+CMGS=+447817814149
44797672147044781781414929/02/2012 16:47:06SMS0 bonjour
44797672146744781781414929/02/2012 16:49:29SMS0

Member Avatar
diafol

Well those outputs seem to match the patterns. SO I'm a little lost as to why it fails:

foreach($request as $msg){
  echo $msg . '<br/>';
  preg_match('/^(\d+)(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS0(.*)/',$msg,$matches);
  echo "<pre>";
  print_r($matches);
  echo "</pre>";
}

That fails?

$m2m_msg = new Soap_connect;
	#get the messages 
	$get_msg = $m2m_msg->peekmessage();
	$number_to_match = '44797672146744781781414929';
	foreach($get_msg as $msg){
		preg_match('/^(\d+)(\d{2}\/\d{2}\/\d{4}) (\d{2}:\d{2}:\d{2})SMS0(.*)/',$msg,$matches);
		if($msg[0] == $number_to_match)echo $msg[4] . "<br />";
		echo $msg . "<br/>";
		#echo "phone:". $matches[1];
		echo "<pre>";
		print_r($matches);
		echo "</pre>";
		#echo "Date/Time: " . $msg[2] . "<br />";
		#echo "Message: <br />" . $msg[3];
		}

output :


44797672146744781781414929/02/2012 16:43:30SMS0 ATE0 AT+CMGS=+447817814149
Array
(
)
44797672147044781781414929/02/2012 16:47:06SMS0 bonjour
Array
(
)
44797672146744781781414929/02/2012 16:49:29SMS0
Array
(
)