0

I get a string like this: Last Draw - 11 10 12 I need to replace Last Draw - 11 10 12 with simple 11.10.12. So I need to remove the "Last Draw" phrase and put dots between the numbers.

I tried several times and I got either wrong result or error.

$result = preg_replace('LAST DRAW', "", $result );

Last time I tried this code: $result = preg_replace('LAST DRAW', "", $result ); with no result.

Thanks for any advice!

1
  • your code example has no occurence of either the string or any preg_replace/str_replace? What is it good for? consider editing your question and focussing on what you want to do with your string. Commented Apr 2, 2014 at 8:41

3 Answers 3

1

Try str_replace()

$string = "Last Draw - 11 10 12";
$var1 = str_replace("Last Draw - ", "", $string);
$var2 = str_replace(" ", ".", $var1);

echo $var2;

Result will give you 11.10.12

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

4 Comments

Thank you, but the problem is that date could be any not just 11 10 1
@user3467607 What would be possible dates? Could you edit your question and define what date formats can be expected?
@Andresch Serj The date format: 07 07 07 (or any other value) Need: 07.07.07 or 07.01.01 etc.
@user3467607 I do not mean what numbers will be expected, i asked for the format. Will you have dates like 3 12 2014 or 23 4 99 or even 7 7 '89? Will there possibly be different seperators than spaces? Like 12-12-1999 or 13/4/04?
0

You are using the preg_replace incorrectly (invalid pattern).

Do this:

$result = preg_replace('/Last Draw - /', '', $result);

or simply use string replace:

$result = 'Last Draw - 11 10 12';
echo str_replace('Last Draw - ', '', $result); // outputs: 11 10 12

2 Comments

Thanks! But the date could be any not just 11 10 12
so? I am only replacing Last Draw - with nothing, leaving what ever else comes after it as it is.
0

Try this:

<?php
$regexSearch = '/Last Draw - ([0-9]{1,2}) ([0-9]{1,2}) ([0-9]{1,4})/'; 
$regexReplace = '$1.$2.$3';
$str = 'Last Draw - 11 10 12
Last Draw - 2 12 2014
Last Draw - 24 3 99
'; 

echo preg_replace($regexSearch, $regexReplace, $str);

you can see it running here.

It basically searches for any dates that look like 1 to two numbers for the first and second value and 1 to 4 for the third value. You could use the following code to have any numbers for the three terms:

/Last Draw - ([0-9]{1,}) ([0-9]{1,}) ([0-9]{1,})/

You can use regex101 to learn to write such regex code and test it directly on their website.

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.