REGEX: needed in php [duplicate] - php

This question already has an answer here:
How to extract individual elements from a JSON string?
(1 answer)
Closed 5 years ago.
{
"status": "success",
"data": {
"title": "test2",
"json_query": {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "epub"
}]
}]
}, {
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "arts"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "automotive"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "business"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}]
}]
}]
}]
}]
}
}
}
This is my string and i need the regex which will get all id with its value
eg: "id" : "event", "id" : "event", "id" : "kwd", "id" : "kwd"
means every occurrence, so i can replace or add a unique value to id
NOTE:- I dont want to make an array, dont want to do with loop,
finding possibility to replace with preg_match or any other regex method.

I got my answer myself:
preg_match_all('/"id":"(.*)",/U', $json, $matches, PREG_PATTERN_ORDER);
print_r($matches); die;

Related

Elasticsearch batch not indexing all items using PHP client

I've been trying to build an index in ES and add the initial items to it (around 350k), using PHP.
I tried all kinds of batch sizes (from 10 items to 1k), check the count, check the threshold, but for some reason it doesn't index every item.
It just skips over some random items, without any errors in the batch result response. I feel like I tried everything and I have to idea what to do next
I'm using Amazon OpenSearch with the latest supported ES (7.10).
The index looks like this:
{
"wonder-search": {
"aliases": {},
"mappings": {
"properties": {
"address": {
"type": "text"
},
"city": {
"type": "text"
},
"city_id": {
"type": "integer"
},
"duration": {
"type": "integer"
},
"filename": {
"type": "text"
},
"geo_point": {
"type": "geo_point"
},
"icon": {
"type": "keyword"
},
"is_sandbox": {
"type": "integer"
},
"item_id": {
"type": "integer"
},
"item_label": {
"type": "keyword"
},
"latitude": {
"type": "float"
},
"longitude": {
"type": "float"
},
"search_text_caption_json": {
"type": "text",
"index_phrases": true
},
"search_text_city_json": {
"type": "text",
"index_phrases": true
},
"search_text_completion": {
"type": "completion",
"analyzer": "simple",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"contexts": [
{
"name": "type",
"type": "CATEGORY"
}
]
},
"search_text_country_json": {
"type": "text",
"index_phrases": true
},
"search_text_cuisine_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_location_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_state_json": {
"type": "text",
"index_phrases": true
},
"search_text_tag_name_json": {
"type": "text",
"index_phrases": true
},
"search_text_username_json": {
"type": "text",
"index_phrases": true
},
"sort": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sort_score": {
"type": "double"
},
"type": {
"type": "text"
},
"user_icon": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_id": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"vanity_url": {
"type": "keyword"
},
"video_count": {
"type": "integer"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"mapping": {
"ignore_malformed": "true"
},
"number_of_shards": "1",
"provided_name": "wonder-search",
"creation_date": "1671003076106",
"number_of_replicas": "1",
"uuid": "YQh1q40WTneLE4MWDWhArw",
"version": {
"created": "7100199"
}
}
}
}
}
and one item looks like this:
{
"_index": "wonder-search",
"_type": "_doc",
"_id": "wq2LD4UBUAuy7FQPhtZh",
"_version": 1,
"_seq_no": 2003,
"_primary_term": 1,
"found": true,
"_source": {
"sort": "4004",
"item_id": "4934",
"user_id": "434",
"user_icon": "/site-content/avatars/Sp8AXjTJvMbRao2oZbuiUuSVH042-1597776099045.jpeg",
"username": "chuurros",
"item_label": "Kyoto Katsugyu【京都勝牛】",
"search_text_username_json": [
"chuurros"
],
"search_text_caption_json": [
"Absolutely love their gyukatsu (beef katsu) here! Delicious and will keep you wanting more! 🥰"
],
"search_text_city_json": [
"Toronto"
],
"search_text_state_json": [
"Ontario"
],
"search_text_country_json": [
"Canada"
],
"search_text_location_name_json": [
"Kyoto Katsugyu【京都勝牛】"
],
"search_text_tag_name_json": [
"japanese",
"restaurant",
"asian",
"dining",
"topcollection-4934"
],
"search_text_cuisine_name_json": [],
"type": "video",
"vanity_url": "",
"icon": "",
"city": "Toronto",
"city_id": "439",
"latitude": "43.65682410",
"longitude": "-79.37617410",
"address": "134 Dundas St E",
"duration": "9.57",
"video_count": "0",
"sort_score": "43",
"filename": "373d75fd-4292-4e5b-a239-4b1c39ffc86c.MOV",
"is_sandbox": "0",
"geo_point": {
"lat": "43.65682410",
"lon": "-79.37617410"
},
"search_text_completion": {
"input": [
"Kyoto Katsugyu【京都勝牛】"
],
"contexts": {
"type": [
"video"
]
}
}
}
}
Any ideas why does it work like this?

Get a value of an array [duplicate]

This question already has answers here:
How to parse JSON and access results
(3 answers)
Closed 1 year ago.
I got the following array called $resource:
{
"id": 24927,
"availability": [],
"block_cost": 0,
"name": "Nachtklettern 11.08.2020",
"parent_id": 0,
"qty": "20",
"sort_order": 0,
"meta_data": [{
"id": 15548,
"key": "kor_reg_status",
"value": "off"
}, {
"id": 15549,
"key": "qty",
"value": "20"
}, {
"id": 15550,
"key": "_arb_reservation_availability",
"value": []
}, {
"id": 15572,
"key": "kor_reg_status",
"value": "off"
}, {
"id": 15573,
"key": "qty",
"value": "20"
}, {
"id": 15574,
"key": "_arb_reservation_availability",
"value": []
}, {
"id": 32463,
"key": "_fusion",
"value": []
}, {
"id": 96581,
"key": "_edit_lock",
"value": "1615588526:3"
}, {
"id": 97192,
"key": "_edit_last",
"value": "3"
}]
}
and want to get the first value of qty -> 20
If I try
echo $resource[5] or
echo $resource["qty"]
I didn't get the value.
What can I do?
Decode the JSON string first, then retrieval of any property is straightforward:
$resource = '{"id":24927,"availability":[],"block_cost":0,"name":"Nachtklettern 11.08.2020","parent_id":0,"qty":"20","sort_order":0,"meta_data":[{"id":15548,"key":"kor_reg_status","value":"off"},{"id":15549,"key":"qty","value":"20"},{"id":15550,"key":"_arb_reservation_availability","value":[]},{"id":15572,"key":"kor_reg_status","value":"off"},{"id":15573,"key":"qty","value":"20"},{"id":15574,"key":"_arb_reservation_availability","value":[]},{"id":32463,"key":"_fusion","value":[]},{"id":96581,"key":"_edit_lock","value":"1615588526:3"},{"id":97192,"key":"_edit_last","value":"3"}]}';
$res = json_decode($resource);
echo $res->qty; // 20

json decode won't work on string with array

I'm trying to import something from an .xml file that I have no control over. Everything went fine until the content:encoded part.
I'm getting the xml like this to strip it of the CDATA Tags:
$xml_object = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
The looks like this, I'm trying to get rid of all the tags and just get the "contents.body" parts, but when I try to use json_decode($itemJson); it returns null. What should I do?
[{
"id": 34543,
"type": "0",
"order": 0,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 345654,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://link.com/1567819/98/eipej334.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 2131656668,
"type": "0",
"order": 1,
"width": 100,
"height": 67.82337662337663,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 1650030480,
"order": 0,
"contents": [{
"type": "media",
"data": [{
"type": "width",
"value": "1540"
}, {
"type": "height",
"value": "1026"
}, {
"type": "videoSrc",
"value": ""
}, {
"type": "src",
"value": "https://links.com/jf/819/156722819/34/343.jpg"
}, {
"type": "alt",
"value": ""
}, {
"type": "mirror",
"value": "false"
}, {
"type": "zoom",
"value": "100"
}, {
"type": "rotate",
"value": "0"
}, {
"type": "positiony",
"value": "50"
}, {
"type": "positionx",
"value": "50"
}, {
"type": "url",
"value": ""
}, {
"type": "borderradius",
"value": "0"
}, {
"type": "sticky",
"value": "false"
}, {
"type": "metapic",
"value": "{\"id\":0,\"tags\":{}}"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.50097,
"widthHeightRatio": 1.47442
}]
}, {
"id": 1740427509,
"type": "0",
"order": 2,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 802708308,
"order": 0,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p>Lorem ipsum dolor sit amet.</p>"
}, {
"type": "html",
"value": "false"
}]
}],
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818
}]
}, {
"id": 79506641,
"type": "0",
"order": 3,
"width": 100,
"height": 55,
"data": {
"areaMargin": 120,
"contentWidthOriginalPx": 760
},
"areas": [{
"id": 28476702,
"order": 0,
"width": 100,
"height": 100,
"widthHeightRatioContent": 1.85874,
"widthHeightRatio": 1.81818,
"contents": [{
"type": "body",
"data": [{
"type": "src",
"value": "<p><span style="\
"font-weight:"
bold;\
"="
"><br></span></p><span style="\
"font-weight:"
bold;\
"="
">lorem upisn? </span>"
}, {
"type": "html",
"value": "false"
}]
}]
}]
}]

Count Json elements wiht PHP

So I have
{
"members": [
{
"username": "John",
"status": "offline",
"avatar_url": "...",
"id": "830232882252102064"
},
{
"username": "Momo",
"status": "online",
"avatar_url": "...",
"id": "259137993351102464"
}
]
}
How do I count (in php) how many users are offline and how many users are online and return them into a value like $memonline and $memoffline.
Here in below code we are getting values of all statuses and then we are counting values of all statuses.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='{
"channels": [
{
"position": 13,
"id": "304700935878213642",
"name": "20KBPS"
},
{
"position": 12,
"id": "304700895978061835",
"name": "50KBPS"
},
{
"position": 11,
"id": "304701193261809672",
"name": "70KBPS"
},
{
"position": 10,
"id": "304701326288224256",
"name": "90KBPS"
},
{
"position": 1,
"id": "304699877621891072",
"name": "=================="
},
{
"position": 9,
"id": "304700570592346114",
"name": "=================="
},
{
"position": 4,
"id": "304701407221514240",
"name": "=================="
},
{
"position": 14,
"id": "304700808883339264",
"name": "=================="
},
{
"position": 2,
"id": "304700525939523584",
"name": "Channel 1"
},
{
"position": 3,
"id": "304700547426942976",
"name": "Channel 2"
},
{
"position": 0,
"id": "304692483973971990",
"name": "General Channel"
},
{
"position": 6,
"id": "304701637446991873",
"name": "Private/2"
},
{
"position": 5,
"id": "304701480605319178",
"name": "Private/2"
},
{
"position": 7,
"id": "304701680010788866",
"name": "Private/3"
},
{
"position": 8,
"id": "304701738999611394",
"name": "Private/3"
},
{
"position": 15,
"id": "304700776691793921",
"name": "Trash Bin"
}
],
"instant_invite": null,
"id": "304692483973971989",
"members": [
{
"username": "Dyno",
"status": "online",
"bot": true,
"game": {
"name": "dynobot.net | ?help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/155149108183695360/5aeb68c29b56b3d92eddb6f46df5051c.jpg",
"avatar": "5aeb68c29b56b3d92eddb6f46df5051c",
"discriminator": "3861",
"id": "155149108183695360"
},
{
"username": "Momo",
"status": "online",
"bot": true,
"game": {
"name": "$help | $info"
},
"avatar_url": "https://cdn.discordapp.com/avatars/259137993351102464/a3005ab7aff3eb829fecf375931a76f1.jpg",
"avatar": "a3005ab7aff3eb829fecf375931a76f1",
"discriminator": "4649",
"id": "259137993351102464"
},
{
"username": "Ronny Dark",
"status": "online",
"avatar_url": "https://cdn.discordapp.com/avatars/152855546574143492/6d9b98972ca6f0308be4dd2aec5aaca3.jpg",
"avatar": "6d9b98972ca6f0308be4dd2aec5aaca3",
"discriminator": "1480",
"id": "152855546574143492"
}
],
"name": "Server name"
}';
$array=json_decode($string,true);
$result=array_column($array["members"],"status");
$members=array_count_values($result);
echo isset($members["offline"]) ? $members["offline"] : 0;

Parsing Rome2Rio json file with PHP

I am trying to extract a segment from the json file of Rome2Rio API with PHP but I cant get an output.
The json file from rome2rio:
{
"serveTime": 1,
"places": [
{ "kind": "town", "name": "Kozani", "longName": "Kozani, Greece", "pos": "40.29892,21.7972", "countryCode": "GR", "regionCode": "ESYE13" },
{ "kind": "city", "name": "Thessaloniki", "longName": "Thessaloniki, Greece", "pos": "40.64032,22.93527", "countryCode": "GR", "regionCode": "ESYE12" }
],
"airports": [],
"airlines": [],
"aircrafts": [],
"agencies": [{
"code": "KTEL",
"name": "KTEL",
"url": "http://www.ktelbus.com/?module=default\u0026pages_id=15\u0026lang=en",
"iconPath": "/logos/Trains/KTELgr.png",
"iconSize": "27,23",
"iconOffset": "0,0"
}
],
"routes": [
{ "name": "Bus", "distance": 121.04, "duration": 120, "totalTransferDuration": 0, "indicativePrice": { "price": 9, "currency": "EUR", "isFreeTransfer": 0 },
"stops": [
{ "name": "Kozani", "pos": "40.30032,21.79763", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" },
{ "name": "Thessaloniki", "pos": "40.6545,22.90233", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" }
]
The PHP code I wrote is:
$json_rome2rio = file_get_contents("http://free.rome2rio.com/api/1.2/json/Search?key=&oName=kozani&dName=thessaloniki");
$parsed_json_r = json_decode($json_rome2rio);
echo $parsed_json_r->agencies->name;
The agencies property contains an array of agencies (note the square brackets). To access the name as you're after, you can do the following:
echo $parsed_json_r->agencies[0]->name;
This assumes that at least one agency is returned and that the agency you are after is the first one if more than one is returned.

Categories