0

HI I have creating a mobile SMS apps

User can get message in his/her mobile through SMS.

I have 5000 messages in my database. But some messages have contact number

See below message:

Success Is Not Permanent & Failure Is Not Final. So,
Never Stop Working After Success & Never Stop Trying After Failure...
1234567890

OR

Success Is Not Permanent & Failure Is Not Final.
01234567890
So, Never Stop Working After Success & Never Stop Trying After Failure...

OR

+911234567890 Success Is Not Permanent & Failure Is Not Final. So,
Never Stop Working After Success & Never Stop Trying After Failure...

I want to remove or get contact number in above 3 format 1234567890 01234567890 +911234567890

Developer
  • 2,676
  • 8
  • 43
  • 65
Chinmay235
  • 3,236
  • 8
  • 62
  • 93
  • `$mystring = preg_replace('/\+?\d{9,11}/','',$mystring);` – Mark Baker Jan 04 '14 at 11:35
  • http://stackoverflow.com/questions/14236148/remove-numbers-from-string-elegant-solution – R R Jan 04 '14 at 11:41
  • The code in @MarkBaker comment seems to leave the final 0 on the third example you provide. It also removes any + symbols. So if a message said "beer + burger is happiness" you would lose the +. As suggested by Marcins answer below, it's the last line that is the issue. Just explode the string by ... and keep the first array chunk, then update the fields. – The Humble Rat Jan 04 '14 at 11:49
  • Yes, I miscounted the number of digits in the number; but it certainly shouldn't remove a `+` unless followed by a number – Mark Baker Jan 04 '14 at 11:53
  • 1
    @TheHumbleRat: This should solve that issue: `/(?:\+\d)?\d{9,12}/`. See demo: http://regex101.com/r/vP9gB4 – Amal Murali Jan 04 '14 at 12:06

2 Answers2

2

If this is a one-off procedure run a query (pseudo code)

SELECT id,message FROM messages

while query $row

$regex = '/\+?\d{9,13}/';

$insertString = preg_replace($regex,'',$row['message']);

UPDATE Messages
SET message = $insertString
WHERE id = $row['id']
LIMIT 1;

end while

Obviously this won't work above but it gives you the code structure and regex you need to achieve your task.

LIMIT 1 is important!

Don't forget to back-up your database before you do this!

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
0

Seems it is always last line of the message, so just remove last line.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141