0

Launguage used PHP TimeString = 1500-1550

I want to use preg_replace to change the 1550 half of the string to 1530. I tried to use a pattern of (/d|/d/d)50 Where I am getting stuck is I don't know how to use the replacement feature to change any /d50 or /d/d50 to have a 30 suffix. I don't want to sue str replace in php as it will change the first time 1500 to 130 since the 50 matches. Any ideas?

1
  • You mean \d instead of /d? Commented Mar 19, 2013 at 19:46

3 Answers 3

2

Here..

<?php
    $numbers = array (1500, 1550, 140, 150, 15000);
    foreach ($numbers as $number) {
        echo preg_replace('/(\d|\d\d)50\b/', '${1}30', $number);
        echo "\n";
    }
?>

Will return:

1500
1530
140
130
15000
Sign up to request clarification or add additional context in comments.

1 Comment

site won't let me upvote until I build my reputation otherwise I would. Thanks for the help!
1

To match digits you need to use \d. Not /d.

Comments

0
preg_replace('/(\d\d)50/', '${1}30', $string)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.