Split string into HTML entities - php

I'm trying to use PHP to create a JSON representation of a paragraph of text, keeping information about links/formatting etc.
Essentially, I want to convert this string:
"Hello <a href='www.google.com'>World!</a>. How are <b>you</b> today?"
Into these 7 JSON objects:
"1": {
"_id": "1",
"_type": "TEXT",
"value": "Hello "
},
"2": {
"_id": "2",
"_type": "TEXT",
"value": "World!",
"_attributes": {
"3": {
"_id": "3",
"_type": "LINK",
"src": "www.google.com"
}
}
},
"4": {
"_id": "4",
"_type": "TEXT",
"value": " How are "
},
"5": {
"_id": "5",
"_type": "TEXT",
"value": "you",
"_attributes": {
"6": {
"_id": "6",
"_type": "FORMATTING",
"bold": true,
}
}
},
"7": {
"_id": "7",
"_type": "TEXT",
"value": " today?"
}
I've hunted the internet/google and found plenty about splitting HTML, but can't seem to describe what I want. I need to separate the plain text from the link/formatting and create a single entity for each.
The "FORMATTING" attribute just adds "bold"/"underline"/"subscript" etc fields as appropriate.
Nested tags will simply create multiple attributes for their text entity.
I don't yet know how I'd handle a 2-word hyperlink that has one word bolded... perhaps it'll have to have 2 hyperlink attributes.
Any help MUCH appreciated!!

A DOMDocument is what you need. If you can live with slightly different names, you barely have to do any work, too.

Related

Instead Join in SQL what should use in MongoDB

I have two collection in MongoDB database, i want join two Collection in PHP
I have searched but unfortunately I have not found a compelling answer.
Data look like this:
users
{
"_id": "4ca30369fd0e910ecc000006",
"login": "user11",
"pass": "example_pass",
"date": "2017-12-15"
}
news
"_id": "4ca305c2fd0e910ecc000003",
"name": "news 333",
"content": "news content",
"user_id": "4ca30373fd0e910ecc000007",
"date": "2017-12-15"
}
Already answer in this thread
Note : I'm a MEAN developer
In mean we use .populate() method (mongoose) to achieve joins upto a level.
as for php
You can use different approach from RDBMS
Data Replication
"news": {
"_id": "4ca305c2fd0e910ecc000003",
"name": "news one",
"content": "news one",
"user": {
"_id": "4ca30369fd0e910ecc000006",
"login": "user11"
},
"date": "2017-12-15"
}

Member Feed in Mongo DB

Which is better in MongoDB to do for member_feed Collection ?
{
"member_id": "153442",
"recent_activity": {
"content_id": "196004",
"content_type_id": "10",
"social_network_id": "9",
},
"_id": ObjectId("5352958667fa3812548e65da"),
"_type": {
"0": "Altibbi_Mongo_MemberFeed"
}
And repeat this object but with different "recent_activity " Object each time an activity happen in social network ,OR to build 1 Document for each member like this and array of recent activity objects
{
"member_id": "153442",
"recent_activity": {
"content_id": "196004",
"content_type_id": "10",
"social_network_id": "9",
},
"recent_activity": {
"content_id": "196005",
"content_type_id": "10",
"social_network_id": "9",
},
"recent_activity": {
"content_id": "196004",
"content_type_id": "10",
"social_network_id": "9",
},
"_id": ObjectId("5352958667fa3812548e65da"),
"_type": {
"0": "Altibbi_Mongo_MemberFeed"
}
Which is better For IO , Inserting , Updating and Selecting . I am using beanstalkd for queuing ?
In case you have a lot of activity records per user you should store it in separate documents (and not in an array as part of one document). Reason: there is a hard limit on document size (16MB).

Loop through JSON from jquery getJSON

Here is the code:
$.getJSON( base_url + '/ajax/sortListings.php', { sort: sort }, function( data ) {
$.each(data, function(i, json) {
$( '#listings' ).append($('<div>').load( base_url + '/partialviews/listingAdminPrev.php', {
id: json.id,
name: json.name,
logo: encodeURIComponent(json.logo),
address: json.address,
city: json.city,
state: json.state,
zip: json.zip,
phone: json.phone,
email: json.email,
web_link: encodeURIComponent(json.web_link),
services: json.services,
category: json.category,
status: json.status,
created: json.created
} ));
});
});
When I manually go to the sortListings.php file and turn the $_POST variables to $_GET, it works fine. So nothing is wrong with the file. But here it is anyway:
include_once('../../app/scripts/config.php');
$listingObject = Listing::getInstance();
$results = $listingObject->get_listings($_POST['sort']);
echo json_encode($results);
That file returns this:
[
{
"id": "32",
"user_id": "32",
"logo": "32_52a0960ba791c.jpg",
"name": "Anthony Thomas Advertising",
"address": "380 S. Main St.",
"city": "Akron",
"state": "AL",
"zip": "44311",
"phone": "3302536888",
"email": "wayne#anthonythomas.com",
"web_link": "http://www.aol.com",
"status": "1",
"services": "dfhfdhdfh",
"category": "1",
"created": "2013-12-05 09:32:56"
},
{
"id": "20",
"user_id": "10",
"logo": "10_529f96001390d.png",
"name": "Graphic Installation Services",
"address": "2808 Broadway Blvd Unit 1",
"city": "Monroeville",
"state": "PA",
"zip": "15146",
"phone": "3306599898",
"email": "graphic#graphicinstallationservices.com",
"web_link": "",
"status": "4",
"services": "Graphic installation services",
"category": "1",
"created": "2013-12-04 11:35:11"
},
{
"id": "21",
"user_id": "10",
"logo": "10_529f9c1a8375d.png",
"name": "Intellect Productions",
"address": "2915 13th St NW",
"city": "Canton",
"state": "OH",
"zip": "44708",
"phone": "3309334833",
"email": "mouseywings#live.com",
"web_link": "",
"status": "1",
"services": "Car Wrap Installations by Intellect Productions",
"category": "1",
"created": "2013-12-04 15:20:15"
},
{
"id": "19",
"user_id": "10",
"logo": "10_529cf170b08d7.png",
"name": "International Installations Inc",
"address": "833 Wooster Rd N",
"city": "Barberton",
"state": "OH",
"zip": "44203",
"phone": "3306586526",
"email": "internationalinstallers#internationinstallers.com",
"web_link": "http://intellectproductions.com/",
"status": "1",
"services": "We install:\r\n· vehicle wraps\r\n· decals\r\n· vehicle lettering\r\n· banners\r\n· billboards\r\n· murals\r\nInternational Image Application Inc. is PDAA certified. We strive to deliver a constant flow of high quality work using best materials in the business, and unsurpassed skill. This ensures that our clients receive value for money, and more bang for their buck!\r\nAnd to ensure that our clients continue to receive the highest quality of work possible, we stay on top of technological trends, new materials, and installation techniques. This dedication to continued education has resulted in many clients coming back again and again.\r\nFrom a simple vehicle wraps installation to an entire ad campaign or fleet, International Image Application Inc. is your destination for precision graphics installation on virtually any medium.",
"category": "1",
"created": "2013-12-04 10:32:52"
}
]
Which is 5 listings.. so it's pulling the data fine. However, nothing is working beyond the $.each().. I even tried alerting stuff and nothing.
No console errors are thrown either..
You hit on your problem. You are expecting the sort parameter in $_POST while you are calling getJSON() which executes a GET.
Make up your mind whether you want to use GET or POST and be consistent.
My guess is you also need to put better error handling in Listing::get_listings() method to provide useful errors when you are not getting the data your expect passed to it.

Which schema is better in web service API design

Recently, our team is going to develop mobile(iphone, android platforms) applications for our existing website, let user can use the application to more easy to read our content via the application.
But our team have different views in JSON schema of the API return, below are the sample response.
Schema type 1:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": {
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
"user": {
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": {
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
},
"user": {
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/2"
},
"post_ts": "2012-12-01 18:15:00T0800"
}
]
}
}
Schema type 2:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": 100,
"user": 1,
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": 101,
"user": 2,
"post_ts": "2012-12-01 18:15:00T0800"
}
],
"category": [
{
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
{
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
}
],
"user": [
{
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
{
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/Hello_baby"
}
]
}
}
Some Developers claim that if using schema type 2,
can reduce data size if the category & user entities comes too much duplicated. it does really reduce at least 20~40% size of response plain text.
once if the data size come less, in parsing it to JSON object, the memory get less
categoey & user can be store in hash-map, easy to reuse
reduce the overhead on retrieving data
I have no idea on it if schema type 2 does really enhanced. Because I read so many API documentation, never seen this type of schema design. For me, it looks like a relational database. So I have few questions, because I have no experience on designing a web services API.
Does it against API design principle (Easy to read, Easy to use) ?
Does it really get faster and get less memory resource on parsing on IOS / Android platform?
Does it can reduce the overhead between client & server?
Thanks you.
When I do such an application for android, I parse JSON just one and put it in database. Later I'm using ContentProvider to access it. In Your case You could use 2nd schema but without user, category part. Use lazy loading instead but it will be good solution just in case categories and users repeat often.

Json Traverse Problem, not able to traverse values

I m getting the below return from ajax call but not able to traverse it please please help.
{
"1": {
"tel1": null,
"status": "1",
"fax": "",
"tel2": null,
"name": "sh_sup1",
"country": "Anguilla",
"creation_time": "2010-06-02 14:09:40",
"created_by": "0",
"Id": "85",
"fk_location_id": "3893",
"address": "Noida",
"email": "sh_sup1#shell.com",
"website_url": "http://www.noida.in",
"srk_main_id": "0"
},
"0": {
"tel1": "Ahemdabad",
"status": "1",
"fax": "",
"tel2": "Gujrat",
"name": "Bharat Petro",
"country": "India",
"creation_time": "2010-05-31 15:36:53",
"created_by": "0",
"Id": "82",
"fk_location_id": "3874",
"address": "THIS is test address",
"email": "bp#india.com",
"website_url": "http://www.bp.com",
"srk_main_id": "0"
},
"count": 2
}
You can do it very easily:
for(i = 0; i < msg.count; i++) {
alert(msg[i]['name']);
}
But the structure of your JSON object is not that good for several reasons:
It does not reflect the structure of the actual data
With this I mean, that you actually have an array of objects. But in your JSON object the elements of the array are represented as properties of an object.
You have invalid JavaScript object property names.
Properties for objects in JavaScript are not allowed to start with numbers. But with msg = { "1": {...}} you have a number as property.
Fortunately it is not that bad because you can access this property with "array like" access msg["1"] (instead of the "normal way", msg.1). But I would consider this as bad practice and avoid this as much as possible.
Hence, as Matthew already proposes, it would be better to remove the count entry from the array on the server side, before you sent it to the client. I.e. you should get a JSON array:
[{
"tel1": "Ahemdabad",
"status": "1",
// etc.
},
{
"tel1": null,
"status": "1",
// etc.
}]
You don't need count as you can get the length of the array with msg.length and you can traverse the array with:
for(var i in msg) {
alert(msg[i].name);
}

Categories