Jump to content

[Help] How would one go about parsing something like so.

Fusion
OK so in my database I have a column in the db that would look like so

 


[2],[3],[51]......

 

I was wondering how I would parse each one to get the number in the middle.

 

New to PHP sorry m8s, no h8s

 

Thanks

Fusion.

life();

Link to comment
Share on other sites

Link to post
Share on other sites

 

OK so in my database I have a column in the db that would look like so
 
[2],[3],[51]......
 
I was wondering how I would parse each one to get the number in the middle.
 
New to PHP sorry m8s, no h8s
 
Thanks
Fusion.

 

I've not worked with PHP, but one way you could do it is to split the string into an array of strings, split by the commas, then iterate over the whole array, and remove the first and last character, assuming they are "[" and "]", then parse what's left. It's a hacky way of doing it, but it works. Another way would be to use regular expressions to find matches of only numbers, store those matches in an array, and read those

Link to comment
Share on other sites

Link to post
Share on other sites

I've not worked with PHP, but one way you could do it is to split the string into an array of strings, split by the commas, then iterate over the whole array, and remove the first and last character, assuming they are "[" and "]", then parse what's left. It's a hacky way of doing it, but it works. Another way would be to use regular expressions to find matches of only numbers, store those matches in an array, and read those

 

Yea I wanted to try to avoid that seeing it's a long string and there would be a lot of pieces to it and could take some time to load.

life();

Link to comment
Share on other sites

Link to post
Share on other sites

function getNumbers($string) {  $matches = array();  $regex = '/\d+/';  preg_match_all($regex, $string, $matches);   return array_pop($matches);                                                   }

Or:

function getNumbers($string) {  $matches = array();  $length = strlen($string);  for ($i = 0; $i < $length; $i++) {    $char = $string[$i];    if (is_numeric($char)) array_push($matches, (int) $char);  }  return $matches;}

Either will return an array of all the numbers in the string. Second method includes cast to integer so that the returned values are actually numbers.

Link to comment
Share on other sites

Link to post
Share on other sites

function getNumbers($string) {  $matches = array();  $regex = '/\d+/';  preg_match_all($regex, $string, $matches);   return array_pop($matches);                                                   }

This will return an array of all the numbers in the string.

 

That was what I was roughly thinking with my second line of thought with regex. I just had no clue how to implement it in PHP specifically.

Link to comment
Share on other sites

Link to post
Share on other sites

That was what I was roughly thinking with my second line of thought with regex. I just had no clue how to implement it in PHP specifically.

 

It's all in the PHP docs. Depending on the length of the string, the second method might be more performant, but I don't know for sure.

Link to comment
Share on other sites

Link to post
Share on other sites

Actually, the second method as written would only match single digits, herp derp. It could be modified to work, but the regular expression is just the most straightfoward way, so no point.

Link to comment
Share on other sites

Link to post
Share on other sites

It's all in the PHP docs. Depending on the length of the string, the second method might be more performant, but I don't know for sure.

I've just never really dabbled in PHP. But I always do try to at least think of a solution that's not language specific, or just pseudo code

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×