Replace comma (,) with dot (.) RegEx php

I try to use this code, but I get this error: No ending delimiter '/' found

 $form = " 2000,50"; $salary = preg_replace('/',', '.'/', $form); // No ending delimiter '/' found echo $salary; 

I am not sure about validating regular expressions.

+6
source share
5 answers

Regex is redundant to replace just one character. Why not just do it instead?

 str_replace(',', '.', $form); 
+52
source
 $salary = preg_replace('/,/', '.', $form); 

But yes, you really don't want to match the pattern, but a string that is constant, so just use str_replace() .

+5
source

You can just use

 str_replace(',','.',$form); 
+3
source

I do not understand your options - I'm not sure what should be in the line and what should not. But for preg_replace, the search pattern must be a string, and the string also starts and ends with a delimiter (usually "/"). I think it needs copies around the search string when it is already inside the string, but how does it work.

The second parameter should contain a line containing a full stop, and nothing more. This gives:

$ salary = preg_replace ('/, /', '.', $ form);

Other people are true that str_replace would be great for turning one character into another, but if the replacement you want is more complicated, preg_replace would be wise.

+1
source

The "/" in your line is used as the delimiter of the beginning of the regular expression, so you need to avoid it. The correct line should look like this:

 $salary = preg_replace('\\/',', '.'/', $form); 

I am also curious why the second parameter is ",". '/', but not ',/'.

EDIT

Ahh Now I see the line should read:

 $salary = preg_replace( '/,/', '.', $form); 

I was confused because the first comma in your example should be ".". to concatenate a string.

0
source

Source: https://habr.com/ru/post/890519/


All Articles