-2

I need a help.. I need to check a string which have a differentcspecific pattern at its 0-5th position. and i need to remove that pattern from that position can u help me in this regard. I am a beginner on php.

For example.

  1. if a number string like "009715012345" i need to crop 00971 from the first 5 position

2.if a number string like "+9715012345" i need to crop +971 from the first 5 position

3.if a number string like "97105012345" i need to crop 9710 from the first 4 position

want to check all condition. When it is inserting to database it should start from 5012345

  • 2
    but why dont you try to crop last 7 digits??? think logically – Nishant Solanki Mar 19 '15 at 12:36
  • 1
    `$newstring = substr('009715012345', -7);` or `$newstring = substr('+97105012345', -7);` or `$newstring = substr('97105012345', -7);` same function for all requirements – Nishant Solanki Mar 19 '15 at 12:36
  • 1
    Secondly, you already laid it out in pseudo code, you know what to do. Throw in some if statements (http://php.net/manual/en/control-structures.if.php) and you should be golden. What do you need from SO, this website is not a replacement for documentation, practice and trying? – Nico Mar 19 '15 at 12:38
  • possible duplicate of [How can I get the last 7 characters of a PHP string?](http://stackoverflow.com/questions/10542310/how-can-i-get-the-last-7-characters-of-a-php-string) – Jeff Lambert Mar 19 '15 at 12:42

1 Answers1

0

You need to use preg_replace function.

$re = "/^(?:(?:\\+|00)971|9710)/m";
$str = "009715012345\n+9715012345\n97105012345";
$subst = "";
$result = preg_replace($re, $subst, $str);
echo $result;

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274