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.

2
  • stackoverflow.com/questions/4366730/… I'm guessing this is what you wanted. Commented Jun 30, 2018 at 13:23
  • Unfortunately not cause "9999" is not always "9999". The only thing that is fixed, that it is always 4 numbers. Commented Jun 30, 2018 at 13:27

5 Answers 5

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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...

4 Comments

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 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'
No, It is always in another place and it is always another number :D
But the autogenerated text contains always only one 4 digits number
0

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

Comments

-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");
}

3 Comments

Sorry but "9999" is always changing. It is not always 9999. :(
@Bence You got your answer already. Did you check Jim's below?
Yes I see it, now I try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.