Finding the right line and get data out of it [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a file that contains lines that look like this:
{"name":"RandomName1","level":20,"class":"something","experience":2688746894},"account":{"name":"RandomAcc1","challenges":{"total":0}}},{"online":false,"rank":172,"dead":false,"character":
{"name":"RandomName2","level":21,"class":"something","experience":2687863942},"account":{"name":"RandomAcc2","challenges":{"total":0}}},{"online":false,"rank":173,"dead":false,"character":
{"name":"RandomAcc3","level":22,"class":"something","experience":2687280914},"account":{"name":"RandomAcc3","challenges":{"total":0}}},{"online":false,"rank":174,"dead":false,"character":
I want to search for a name, for example "RandomAcc3", if the name is found, check the "online" status in that line and extract that into a boolean. Any help is much appreciated!

read the file, json_decode into an array, fetch the array (foreach) and check if the current subelement contains the string (substr/preg_match). if so, you'll consider/return the "online" element. hope you have a bit of coding skills to write it yourself, otherwise use codeacademy, not stackoverflow

$fileContent = file_get_contents($filepath);
preg_match('/"name":"RandomAcc3"(.*)"online":([a-z]+)/i', $fileContent, $matches);
$onlineStatus = ($matches[2] == 'true');

Related

how do I get just one value from mysql php query results [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Below are the results of my query. I would just like to return the refundable_amount value. Which for this query $4.04
a:1:{i:0;a:19:{s:8:"label_id";i:3324543;s:8:"tracking";s:22:"9400136205309684548265";s:17:**"refundable_amount";d:4.04**;s:7:"created";i:1614781854687;s:10:"carrier_id";s:4:"usps";s:12:"service_name";s:23:"USPS - First Class Mail";s:6:"status";s:9:"PURCHASED";s:22:"commercial_invoice_url";N;s:12:"package_name";s:11:"Single Kcup";s:9:"is_letter";b:0;s:13:"product_names";a:1:{i:0;s:28:"Multiple Aware";}s:11:"product_ids";a:1:{i:0;i:1212;}s:15:"receipt_item_id";i:72845982;s:12:"created_date";i:1614781859000;s:11:"expiry_date";i:1630333858000;s:15:"main_receipt_id";i:54663164;s:4:"rate";d:4.04;s:8:"currency";s:3:"USD";s:12:"label_cached";i:1614781863000;}}
Thank you in advance for any help.
You have typo, in near the "Multiple Aware" output, it should be 14, if you correct it like below,
Then you can use php unserialize function
$text = unserialize ('a:1:{i:0;a:19:{s:8:"label_id";i:3324543;s:8:"tracking";s:22:"9400136205309684548265";s:17:"refundable_amount";d:4.04;s:7:"created";i:1614781854687;s:10:"carrier_id";s:4:"usps";s:12:"service_name";s:23:"USPS - First Class Mail";s:6:"status";s:9:"PURCHASED";s:22:"commercial_invoice_url";N;s:12:"package_name";s:11:"Single Kcup";s:9:"is_letter";b:0;s:13:"product_names";a:1:{i:0;s:14:"Multiple Aware";}s:11:"product_ids";a:1:{i:0;i:1212;}s:15:"receipt_item_id";i:72845982;s:12:"created_date";i:1614781859000;s:11:"expiry_date";i:1630333858000;s:15:"main_receipt_id";i:54663164;s:4:"rate";d:4.04;s:8:"currency";s:3:"USD";s:12:"label_cached";i:1614781863000;}}');
you can get the value as below,
echo $text[0]['rate'];
will output
4.04
For more information about this function you can visit
https://www.php.net/manual/en/function.unserialize.php

PHP - Convert RegExp to string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I tried several times to find a solution without an idea of how to approach it.
I want to convert this string: '^3816[4-6].*$';
Into this: '38164, 38165, 38166';
In other words I want to genarate all the possible options of a given string: 'XXXX[2-7]';
So the output should be: 'XXXX2, XXXX3, XXXX4, XXXX5, XXXX6, XXXX7';
please help me to solve this issue if anyone know how to accomplish it.
Use a RegExp ;)
<?php
$string_pattern = '^3816[4-6].*$';
$pattern = '/^\^(.*)\[(.)-(.)\](.*)\$$/';
preg_match($pattern, $string_pattern, $matches);
for ($i = $matches[2]; $i <=$matches[3] ; $i++) echo $matches[1].$i.$matches[4]."\n";
?>
The question is: what really contains $matches[4]?

PHP language arrays and text [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a text box and I wrote some text in it.
the question is, how to put the written text inside an array, and then use the explode function to separate each word ?
first get the value like this (I assume that you have a form and you send it by POST):
$name_of_array = $_POST['name_of_your_form_field'];
$separeted_vars = explode(' ', $name_of_array);
I hope this help you!
p.d: the $name_of_array isn't necessary that be array...
Something like this:
<?php
$words = explode(' ', $_POST['textbox_contents']);
?>
"this is some text to be separated" // this is written in a text box with a button called separate
the array is gonna be used in the explode function
the expected result is
this
is
some
text
to
be
separated

How Retrive A Piece Of Text From A Source Website With PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have to get something that has been shared on a website and it changes every day. but i dont know how to do this. how can i do this?
You could download the exact page you're after using file_get_contents
$web = file_get_contents("http://google.com")
You'd then have to strip out whatever it is you want from the rest of the source code on the website. That can be done by using strpos or stripos (ipos being case insensitive) to find its location, and then using substr to extract that part of the string.

My small code is not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
At the moment, I'm working on a small script.
I'm new at PHP, I've got a problem
I have a field in database and the field content is like this
"1","2","3"
When i put these numbers in array
like this
$users=array("1","2","3");
, my code will work , but when i put these codes in a variable and put variable inside the array like this
$users=array($amanj);
it wont.
Please Help me if possible.
Thanks very much.
It sounds like you need to redesign your database because having "1","2","3" is not a good idea if you really need 3 separate values.
But to answer your question, if you need to convert the string "1","2","3" to an array, you can do:
$users = explode(',', $amanj);

Categories