i am trying this code but i get this error: No ending delim开发者_运维技巧iter '/' found
$form = " 2000,50";
$salary = preg_replace('/',', '.'/', $form); // No ending delimiter '/' found
echo $salary;
I am not sure about the regex validation.
Regex is overkill for replacing just a single character. Why not just do this instead?
str_replace(',', '.', $form);
$salary = preg_replace('/,/', '.', $form);
But yeah, you don't really want to match a pattern but a string which is constant, so simply use str_replace()
.
You can simply use
str_replace(',','.',$form);
I don't understand your parameters -- I'm not sure what's supposed to be in the string and what isn't. But for preg_replace, the search pattern should be a string, and with the string also begin and end with a delimiter (typically '/'). I think it's redudant to need slashes round the search string when it's already inside a string, but that's how it works.
The second parameter should be a string containing the full stop and nothing else. This gives:
$salary = preg_replace( '/,/' , '.' , $form);
Other people are correct that str_replace will be fine for turning one character into another, but if the replacement you want gets more complicated preg_replace will be reasonable.
The '/' in your string is used as a start-of-regex delimiter so you need to escape it. The correct line should read:
$salary = preg_replace('\\/',', '.'/', $form);
I'm also curious why the second param is ', ' . '/' rather than ', /'.
EDIT
Ahh I see now, the line should read:
$salary = preg_replace( '/,/', '.', $form);
I was confused because the first comma in your example should be a '.' to concat the string.
精彩评论