0

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

Edited by moroccanplaya: n/a

3
Contributors
38
Replies
39
Views
5 Years
Discussion Span
Last Post by diafol
Featured Replies
  • 1
    diafol 3,720   5 Years Ago

    You retrieve the data as an array I take it? So, for each peekMessage - if an array, e.g.: [CODE]$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 />"; }[/CODE] Read More

  • 1
    diafol 3,720   5 Years Ago

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

0

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.

0

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]; }
0

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

0

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

Edited by moroccanplaya: n/a

0

here is the output

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

0
$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?

1

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 />";
}
0

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 />";
}

Edited by diafol: n/a

0
$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?

0

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];

Edited by diafol: n/a

0

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"

0

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??

0

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

1

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

0

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.

0

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

echo "number: " . $matches[1];
0

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.

Edited by diafol: n/a

0

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

0

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

Edited by moroccanplaya: n/a

0

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?

0
$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
(
)

Edited by moroccanplaya: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.