0

I have this autogenerated variable:

$var = "WXYZ 300700Z 32011KT 9999 FEW035 SCT200 24/16 Q1007 NOSIG";

How can I search and save "9999" in this var? I cant use substr cause $var's value is always changing and it is always in another "place" in the variable. It is always 4 numbers.

Bence
  • 97
  • 1
  • 8
  • https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word I'm guessing this is what you wanted. – Sebi Ilie Jun 30 '18 at 13:23
  • Unfortunately not cause "9999" is not always "9999". The only thing that is fixed, that it is always 4 numbers. – Bence Jun 30 '18 at 13:27

5 Answers5

3

You can match 4 numbers wrapped by word boundaries or space characters, depending on what you need with regular expression (regex/regexp).

if( preg_match('/\b([0-9]{4})\b/', $var, $matches) > 0 ) {
    // $matches[1] contains the number
}

Note, however, that the word boundary match will also match on non-letter characters (symbols like dollar sign ($), hyphen (-), period (.), comma (,), etc.). So a string of "XYZ ABC 9843-AB YZV" would match the "9843". If you want to just match based on numbers surrounded by white space (spaces, tabs, etc) you can use:

if( preg_match('/(?:^|\s)([0-9]{4})(?:\s|$)/', $var, $matches) > 0 ) {
    // $matches[1] contains the number
}
Jim
  • 3,210
  • 2
  • 17
  • 23
1

Using explode is the way to go, we need to turn the string into an array, our variables are separated by white space, so we get a variable every time we face a white space " ", i made another example to understand how explode works.

<?php
$var = "WXYZ 300700Z 32011KT 9999 FEW035 SCT200 24/16 Q1007 NOSIG";
print_r (explode(" ",$var)); //Display the full array.
$var_search = explode(" ",$var);
echo $var_search[3];//To echo the 9999 (4th position).
?>
<br>
<?php
$var = "WXYZ+300700Z+32011KT+9999+FEW035+SCT200+24/16+Q1007+NOSIG";
print_r (explode("+",$var)); //Display the full array.
$var_search = explode("+",$var);
echo $var_search[3];//To echo the 9999 (4th position).
?>

I hop this is what you're looking for

AraByte
  • 154
  • 9
0

Is this viable?

$var = "WXYZ 300700Z 32011KT 9999 FEW035 SCT200 24/16 Q1007 NOSIG";
if (strpos($var, '9999') == true {
    // blah blah
}
else{
    echo 'Value not found'
}

Personally haven't tested this yet, but I think you're looking for something along these lines...

jam
  • 1
  • 2
  • Sorry but it isnt that I would. The 9999 is always another number. The only thing that is fixed, that is it always 4 numbers. For example: $var = "WXYZ 300700Z 32011KT 4500 FEW035 SCT200 24/16 Q1007 NOSIG"; $var = "WXYZ 300700Z 32011KT 7000FEW035 SCT200 24/16 Q1007 NOSIG"; – Bence Jun 30 '18 at 13:30
  • @Bence do you mean that it's 4 same consecutive numbers? edit: nvm, so it's in the same place but changes numbers? Referring to from '9999' to '4500' – jam Jun 30 '18 at 13:32
  • No, It is always in another place and it is always another number :D – Bence Jun 30 '18 at 13:34
  • But the autogenerated text contains always only one 4 digits number – Bence Jun 30 '18 at 13:36
0

Hello I would use a preg_match regex using this regular expression : \d{4}

andrea06590
  • 1,259
  • 3
  • 10
  • 22
-1

here is the solution

var str1 = "WXYZ 300700Z 32011KT 9999 FEW035 SCT200 24/16 Q1007 NOSIG";
var str2 = "9999";
if(str1.indexOf(str2) != -1){
    console.log(str2 + " found");
}