Question

I have string in PHP like this:

$string = 'Monitor Asus VH228D LED, 21,5", 16:9, 1920x1080, 250 cd/m2; VS228H, 21,5", Wide format 16:9, 1920x1080, VS229H, 21.5 ", Vf3349H, 21,5 ", 1920x1080, 250 cd/m2, Asus VH2q8D LED, 21,5 inches';

I need to find inches in string and if number of inches is incorrect (21,5", 21,5 ", 21.5 ") to change with correct value 21,5" -> 21.5", 21,5 " -> 21.5", 21.5 " -> 21.5" , 21.5inches -> 21.5".

The final result must be:

$final_string = 'Monitor Asus VH228D LED, 21.5", 16:9, 1920x1080, 250 cd/m2; VS228H, 21.5", Wide format 16:9, 1920x1080, VS229H, 21.5", Vf3349H, 21.5", 1920x1080, 250 cd/m2, Asus VH2q8D LED, 21.5"';

How to do that?

Was it helpful?

Solution

If you want to make sure you cover all the different possibilities you highlighted above, use

preg_replace('/(\d+)(,|.)(\d+) *"/i', '$1.$3"',$string);

(I do not believe mossfoot's answer covered the case of a space between the number and the ")

OTHER TIPS

$final_string = preg_replace('/(\d+),(\d+")/', '$1.$2', $string);

Read about regular expressions. A good place to start is http://www.regular-expressions.info/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top