user3467607
user3467607

Reputation: 9

How to replace string in parsing html with php?

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!

Upvotes: 0

Views: 76

Answers (3)

Parag Tyagi
Parag Tyagi

Reputation: 8960

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

Upvotes: 1

Andresch Serj
Andresch Serj

Reputation: 37338

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.

Upvotes: 0

Latheesan
Latheesan

Reputation: 24116

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

Upvotes: 0

Related Questions