1

I have this string: "4702819 - 09" (without the quotes) and i want to make a pattern to change it to : "094702819". So basically to remove the space-space completely and to cut what came after it to be at the beginning. So another example would be:

"5502819 - 098" to "0985502819" etc. How do i do that? do i use preg replace?

2 Answers 2

2
$string = "4702819 - 09";
$pattern = '/(\d+) - (\d+)/i';
$replacement = '$2$1';
print preg_replace($pattern, $replacement, $string);

The above will output 094702819. See the PHP manual on preg_replace() for additional examples.

Sign up to request clarification or add additional context in comments.

1 Comment

@Kristian Antonsen: bad bet;-) if you know what i mean
0

Another way to do it would be:

$string = "4702819 - 09";
$arr = explode(" - ", $string);
$newString = $arr[1].$arr[0];
echo $newString;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.