D
D
Deman12018-04-02 22:41:09
PHP
Deman1, 2018-04-02 22:41:09

How to process text in php?

Hello, tell me how to make the text change well and check.
What do I need?
I pass numbers for example 123412,5123513,312341
I need them to become '123412','5123513','312341' after they are saved
Well, how to make a check that there are only numbers and a comma, but letters are spaces and so on what is not needed just disappeared.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-04-02
@zkelo

Your best bet is to use a regular expression (\d+).

$matches = [];
preg_match_all('/(\d+)/', '56, 38, 39', $matches);

This code will write to the array $matchesall the numbers found in the string. And from the array they can be displayed separated by commas, using, for example, foreach.

Q
Quark_rgb, 2018-04-02
@Quark_rgb

$number = "123412,5123513,312341";
$replacement = "'$1','$2','$3'";
$m = preg_replace("~(\d{6}),(\d{7}),(\d{6})~", $replacement, $number);
print_r($m); // '123412','5123513','312341'

regex101

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question