check a string with php

Asked

Viewed 123 times

-2

I have an input where the user informs several product codes, and the system returns a list.

But I need to validate the string that is to check this exactly as: 132,234,14,56

That is, each code separated by a comma.

I tried to do it that way, but it didn’t work:

if (preg_match('/^,/', $ids)) {
       echo "erro nas informações";
    }

OBS: It has to be always integer numbers, separated by , can not end with , can not contain letters nor space.

  • 6

    Your question is not clear enough, try to give more details.

  • 3

    Are only 4 codes? Are they always 4 codes? Does the number of digits vary? Can you end with ,? Can you have spaces? Can you have letters? These, for example, are some of the basic things that make all the difference when answering.

  • no, the user can enter as many codes as he wants

  • The other questions I asked in the comment are important too. Although apparently these requirements are to solve some other problem in other parts of your code, then the right solution would not be the one you are asking, but at least putting all the details in the question can help you get the answer you expect.

  • Sorry, I really did not know how to express myself in this question. Well come on. It has to be always whole numbers, separated by , cannot end with the , may not contain letters or space.

  • 1

    @Hugoborges search [Dit] the question with this information and some more varied examples than can and cannot. Not everyone reads the comments. This "bureaucracy" that we ask is to help you get where you want, and to avoid that the people waste time responding and the solution does not solve your problem. If you can make the question very clear, it’s easy for the community to vote to reopen the question (and possibly improve the vote). The intention is always to help, but it takes this "Investment" on your part to make the problem as clear as possible.

  • ok I’ve done it ;) vlw

Show 2 more comments

3 answers

1

From what I understand it is a list containing an indeterminate number of integers separated by a comma. So it would look like this:

if(preg_match("/^(\d+,)*\d+/",$ids)===false){
  echo "erro nas informações";
}
  • 1

    I was posting this here, but I think yours is enough.(\d+,)*\d+ (and would put a version with optional spaces, and optional final comma) - anyway, it remains @Hugo better clarify in the question what he wants.

  • I updated the regular expression, now it has optional spaces.

  • 1

    If the questioner confirms that this is what he wants, I think it is a good solution.

  • cannot contain spaces, only integer numbers separated by comma

  • tried the following string 23,23,22 and it didn’t work. But it had to work

  • Because it didn’t work, it has to print some message when the string is valid?

  • well it has to print the error when it is not valid. When it is correct it can do nothing.

  • There was a typo, now it should work!

Show 3 more comments

0

One way is to compare the number of commas in the string and the amount of data that these commas separate, for example:

$string = "132,234,14,56";

if(substr_count($string,",") == (count(array_filter(explode(",",$string))) - 1)){
  echo "Valida";
} else {
  echo "Invalida";
}

In your example, you have three commas that separate four values, substr_count counts the number of commas, and compares with the amount of values by making a explode in the string and then a array_filter to clear voids.

You can also validate the values using a callback function on array_filter, example:

$strings = array("132,234,14,56","132,234,14,56,ABC","132,234,14,56,",",132,234,14,56,ABC","1,2,3");

foreach ($strings as $string){

  if(substr_count($string,",") == (count(array_filter(explode(",",$string),'is_numeric')) - 1)){
    echo "String $string é válida";
  } else {
    echo "String $string é inválida";
  }
  echo PHP_EOL;
}

Ideone

-2

Experiment with the strpos.

if (strpos($mystring, ".") !== false) {
    //código
}
  • but it works with ,?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.