I need to be able to search within a string and find out if [topic] is equal to a number and grab that number only from within the string.
For example, a string like so:
[topic]=10[board]=1
should return 10
But a string like this:
[topic][board]=1
should return 0 or false
A string like this:
[topic]=1.5[board]=2
should return 1, cause we need to round down floor()
Also, we aren’t worried about negative numbers, cause this will never happen.
How can I do this to just grab the number only, rounding down, from these types of strings that look like this, only if [topic] is present in the string and defined with an equal sign.
Thanks guys 🙂
The idea below uses
preg_matchand a regular express that looks for the word "topic" inside square brackets followed by an equal sign and one of more numbers. Before the matches, I set the default value of the topic (falsein this case). If a topic is found, I then convert it to an integer.This will ignore the decimal point and any numbers that follow as
\donly contains the numbers 0 through 9.Example: