Rectangle 27 28

Portability: Winner JSON. JSON is supported on a wider variety of platforms, while PHP de-serialization is only supported (as far as I know) by PHP. While it's possible to parse either format in any language, JSON has more pre-built libraries.

Future Proof: Winner JSON. JSON is a "standard", in the sense that Javascript is a standard, and isn't likely to change anytime in the future. The PHP group has made no promises about the future of the serialization format, and while it's unlikely to change in the future, the fact that a single group controls the format means you may end up with future data that's unreadable.

Fidelity: Winner PHP. PHP serialization will allow you to store data with native PHP data types, including objects defined by custom classes. JSON will only allow you to store generic primitive types, lists of primitive types ("arrays") and key/value pair Objects. PHP Serialization may provide some advantages here if you're developing a PHP application.

File Size: JSON has a slight win here, as PHP's current serialization format is more verbose (as it's storing more information).

Conclusion: Go with JSON unless you have a compelling reason to use PHP Serialization .

php - JSON vs. Serialized Array in database - Stack Overflow

php mysql serialization mysql-json
Rectangle 27 70

Here is a PHP based parser that parses HTML using some ungodly regex. As the author of this project, I can tell you it is possible to parse HTML with regex, but not efficient. If you need a server-side solution (as I did for my wp-Typography WordPress plugin), this works.

htmlawed is another PHP project that parses HTML to filter, convert, etc. Has some nice code if you can figure it out!

html - RegEx match open tags except XHTML self-contained tags - Stack ...

html regex xhtml
Rectangle 27 70

Here is a PHP based parser that parses HTML using some ungodly regex. As the author of this project, I can tell you it is possible to parse HTML with regex, but not efficient. If you need a server-side solution (as I did for my wp-Typography WordPress plugin), this works.

Here is a PHP based parser that parses HTML using some ungodly regex. As the author of this project, I can tell you it is possible to parse HTML with regex, but not efficient. If you need a server-side solution (as I did for my wp-Typography WordPress plugin), this works.

htmlawed is another PHP project that parses HTML to filter, convert, etc. Has some nice code if you can figure it out!

htmlawed is another PHP project that parses HTML to filter, convert, etc. Has some nice code if you can figure it out!

html - RegEx match open tags except XHTML self-contained tags - Stack ...

html regex xhtml
Rectangle 27 1

Is it possible to parse an XML with & in value of NODE ?

No, that means the file is not well-formed XML at all therefore does not really qualify as an XML file and no XML file parser can deal with that otherwise it would not be an XML parser.

However you can pre-process the data before you pass it to an XML parser and fix the issue (& -> ) your own.

php - How to parse XML with unescaped ampersand - Stack Overflow

php xml xmlreader
Rectangle 27 3

The problem your having is this string is NOT Proper JSON: [500,'hello world']

this would be proper JSON [500,"hello world"]. JSON is very strict on formatting and requires that all string values be wrapped in double quotes and NEVER single quotes.

the proper thing to do to avoid problems, would be to use the php functions json_encode() and json_decode()

<?php
    $myarray[] = 500;
    $myarray[] = "hello world";
    $myjson = json_encode($myarray);
?>
<form name="input" action="post.php">
    <input type="hidden" name="json" value="<?php echo $myjson ?>" />
    <input type="submit" value="Submit">
</form>

and in the post.php you would read it like so,

<?php
    $posted_data = array();
    if (!empty($_POST['json'])) {
        $posted_data = json_decode($_POST['json'], true);
    }
    print_r($posted_data);
?>

the true flag in json_decode() tells the function you want it as an associative array and not a PHP object, which is its default behavior.

the print_r() function will output the php structure of the converted JSON array:

Array(
    [0] => 500
    [1] => hello world
)

Good catch. However you also need to use single quotes then for the value='..' attribute, or better yet apply htmlspecialchars(), so the JSONs double quotes don't break the HTML syntax.

oops, yeah was going a bit fast, good catch yourself. forgot about the quotes on the input tag lol. If you wanted to avoid the special characters and avoid the single quotes on the input, you could just base64_encode the json, then on the other end base64_decode it to avoid any complications. similar to how facebook does their posted json signed request on load of a facebook application.

php - Is it possible to send a json array via post? - Stack Overflow

php html json post
Rectangle 27 3

The problem your having is this string is NOT Proper JSON: [500,'hello world']

this would be proper JSON [500,"hello world"]. JSON is very strict on formatting and requires that all string values be wrapped in double quotes and NEVER single quotes.

the proper thing to do to avoid problems, would be to use the php functions json_encode() and json_decode()

<?php
    $myarray[] = 500;
    $myarray[] = "hello world";
    $myjson = json_encode($myarray);
?>
<form name="input" action="post.php">
    <input type="hidden" name="json" value="<?php echo $myjson ?>" />
    <input type="submit" value="Submit">
</form>

and in the post.php you would read it like so,

<?php
    $posted_data = array();
    if (!empty($_POST['json'])) {
        $posted_data = json_decode($_POST['json'], true);
    }
    print_r($posted_data);
?>

the true flag in json_decode() tells the function you want it as an associative array and not a PHP object, which is its default behavior.

the print_r() function will output the php structure of the converted JSON array:

Array(
    [0] => 500
    [1] => hello world
)

Good catch. However you also need to use single quotes then for the value='..' attribute, or better yet apply htmlspecialchars(), so the JSONs double quotes don't break the HTML syntax.

oops, yeah was going a bit fast, good catch yourself. forgot about the quotes on the input tag lol. If you wanted to avoid the special characters and avoid the single quotes on the input, you could just base64_encode the json, then on the other end base64_decode it to avoid any complications. similar to how facebook does their posted json signed request on load of a facebook application.

php - Is it possible to send a json array via post? - Stack Overflow

php html json post
Rectangle 27 2

Sounds like you're asking how to draw an image using your JSON and then get a data uri for it.

You could use the following

<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
  var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');

  var jsonObj = [{x:1, y:2}, {x:3,x:4}];

  // draw using your data, you implement this
  context.beginPath();
  context.moveTo(jsonObj[0].x, jsonObj[0].y);
  context.moveTo(jsonObj[1].x, jsonObj[1].y);
  context.closePath();
  context.lineWidth = 5;
  context.fillStyle = '#8ED6FF';
  context.fill();
  context.strokeStyle = '#0000ff';
  context.stroke();

  // save canvas image as data url (png format by default)
  var dataURL = canvas.toDataURL();
  alert(dataURL);
</script>

Is it possible to do this in PHP? The JSON output above is currently captured within a PHP variable ($output).

@Rap Feel free to add an answer with a PHP snippet. I'd rather keep the answer about what the question was. And it's pretty trivial to get JSON from PHP, another question if you don't know how to do that.

javascript - Is it possible to convert JSON into a Data URI with PHP o...

php javascript json data-uri
Rectangle 27 1

Is the API a 3rd party on or made by you?

If it is yours, consume the data sent by your form should be as simple as this on your API:

<?php
    $data = json_decode($_REQUEST['json']);

    echo $data[0]; // Should output 500
    echo $data[1]; // Should output hello world

If it is a 3rd party, probably they expect you to send the json in post body. To accomplish that, follow this post: How to post JSON to PHP with curl.

php - Is it possible to send a json array via post? - Stack Overflow

php html json post
Rectangle 27 1

Is the API a 3rd party on or made by you?

If it is yours, consume the data sent by your form should be as simple as this on your API:

<?php
    $data = json_decode($_REQUEST['json']);

    echo $data[0]; // Should output 500
    echo $data[1]; // Should output hello world

If it is a 3rd party, probably they expect you to send the json in post body. To accomplish that, follow this post: How to post JSON to PHP with curl.

php - Is it possible to send a json array via post? - Stack Overflow

php html json post
Rectangle 27 3

When and if possible, you should always use a DOM libraries in PHP to parse HTML.

$dom = new DOMDocument;
$dom->loadHTML($html);
$lis = $dom->getElementsByTagName("li");
$index = 0;
foreach ($lis as $li) {
    $index++;
    $value = (string) $li->nodeValue;
    if ($value == "Result 4") {
        echo $index;
    }
}

where $html has the entire HTML in a string (result of your file_get_contents function). If the HTML is dirty, you can use the PHP function tidy_repair_string to repair it.

php find string in html - Stack Overflow

php html file-get-contents strpos
Rectangle 27 1

It supports PHP, JavaScript/JSON, HTML, CSS, SCSS, Python, Visual Basic/VBScript and maybe more in the feature. For PHP formatting it uses a fork of the phpfmt which works well. All languages-specific formatting can be set in one config file.

sublimetext2 - Is it possible to auto format PHP in Sublime Text 2? - ...

php sublimetext2 autoformatting
Rectangle 27 1

It supports PHP, JavaScript/JSON, HTML, CSS, SCSS, Python, Visual Basic/VBScript and maybe more in the feature. For PHP formatting it uses a fork of the phpfmt which works well. All languages-specific formatting can be set in one config file.

sublimetext2 - Is it possible to auto format PHP in Sublime Text 2? - ...

php sublimetext2 autoformatting
Rectangle 27 31

It is possible to convert integer and float columns back to PHP numbers by setting the MYSQLI_OPT_INT_AND_FLOAT_NATIVE connection option, if using the mysqlnd library. If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it. This way, for example, SQL INT columns are returned as integers.

MySQL Client Library will return all fields as strings.

Another solution would be to use json_encode options with JSON_NUMERIC_CHECK.

return response()->json(["foo" => "bar"], 200, [], JSON_NUMERIC_CHECK);
apt-get remove php5-mysql
apt-get install php5-mysqlnd
service apache2 restart

php - Laravel 5 controller sending JSON integer as string - Stack Over...

php json laravel laravel-5 php-5.6
Rectangle 27 31

It is possible to convert integer and float columns back to PHP numbers by setting the MYSQLI_OPT_INT_AND_FLOAT_NATIVE connection option, if using the mysqlnd library. If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it. This way, for example, SQL INT columns are returned as integers.

MySQL Client Library will return all fields as strings.

Another solution would be to use json_encode options with JSON_NUMERIC_CHECK.

return response()->json(["foo" => "bar"], 200, [], JSON_NUMERIC_CHECK);
apt-get remove php5-mysql
apt-get install php5-mysqlnd
service apache2 restart

php - Laravel 5 controller sending JSON integer as string - Stack Over...

php json laravel laravel-5 php-5.6
Rectangle 27 2

You could use Quercus or JSR223 + PHP-bridge to read/parse/run PHP files using Java.

As to MySQL, as being just a simple and standalone DB server, it doesn't require a specific webserver. MySQL totally doesn't care wat webserver you're running. Just install and configure it the usual way.

Oh, please note that the Apache Tomcat Connector requires Apache HTTPD server. So if you don't want to install Apache HTTPD, then the Tomcat Connector is worthless to you.

finally I didn't have enough time to try that, but I chose the other solution, which seems much simpler: to install XAMPP with Tomcat Add-On, which "connects" Tomcat to Apache HTTP Server; but I'm sure your solution works (even if I didn't have a chance (and time) to try it), that's why I accepted it. Thanks. BUT I would be glad if you could help me in the further steps to configure Apache HTTP Server + Tomcat Server the appropriate way: serverfault.com/questions/301224/ . Thanks for that in advance too! :)

Is it possible to install PHP 5 and MySQL on Apache Tomcat 6.0.32 WITH...

php apache jsp tomcat webserver
Rectangle 27 1

I'm not sure whether it's possible to do what you want with one regular expression, but certainly possible with one regular expression per variable. Also it's Possible to use strpos function to parse string. Here the second approach will probably be more efficient way.

php - "You can't parse HTML with regex" WHY? - Stack Overflow

php regex html-parsing
Rectangle 27 2

Or you can use the API of Google Feed to load a RSS Feed and parse it as you need. :) I think is a interesting option.

Sadly, the Google Feed API is now deprecated.

php - Is it possible to get an rss feed using javascript client side s...

php javascript rss cross-domain
Rectangle 27 3

It is possible to build up dynamic data for an AJAX request but clearly you'll need to know the logic for retrieving that dynamic data. You don't describe this in your question, so in the below example I'm assuming it's based on the number of .user_input fields in the form (which could be 2, or 10, or whatever). The data is then comprised of field name + field value.

The point is to show dynamic collection of data, that's all.

var ajax_data = {};
$('.user_input').each(function() {
    ajax_data[$(this).attr('name')] = $(this).val();
});
$.post('some/url.php', ajax_ata); //etc

and one more thing, what should I do if there is multiple fields like name="emial[]"? I think this method won't work for that purpose.

As I said in my answer, the means of compiling your data is up to you and I can't predict that. I assumed a contrived example where you just wanted to gather up the values from user_input fields. I was simply showing that, yes, you can build dynamic AJAX data. , - . 4 .

php - Send post form data in json format via ajax with JQuery dynamica...

php javascript ajax json
Rectangle 27 1

What you printed is not JSON, it's the string representation of a php Object I think. So if possible, you should store into your database actual JSON. It's easier, cleaner and more compact.

To do so, use json_encode($your_object) to insert the data in your database.

This should work like a charm.

Note: There's a trap though, by default, json_decode will create a php Object and not an array. So you have to retrieve your fields as attributes of an object:

$data = json_decode($string_from_database, true);
$data['product']['title']; // to access title in your data structure

Check the php documentation of json_decode for further info!

Thank you this has really helped. I realised I has set json_decode before posting it which was the issue. I am now able to use json_encode when posting it and json_decode to retrieve it. How do I get access to a particular field though? ie: title

Oh yeah, there's a trap, by default, json_decode will create a php Object and not an array. So you have to retrieve your fields as attributes of an object: $data = json_decode($string_from_database); $data->product->title; // to access title in your data structure

But if you want an array, you can provide an argument to get an array like this: json_decode($string_from_database, true) and in that case, you can access the data like this: $data['product']['title'] Ceck the php documentation of json_decode for further info!

Thanks Louis. I realised yesterday we had posted it as a string and not pure JSON. I managed to use what you have posted above and it now works like a treat ;) thank you.

mysql - JSON which is formatted as string, convert to php array? - Sta...

php mysql arrays json
Rectangle 27 68

Unfortunately, it is not possible to call a method on an object just created with new before PHP 5.4.

In PHP 5.4 and later, the following can be used:

$purchaseOrder = (new PurchaseOrderFactory)->instance();

In previous versions, you have to call the method on a variable:

$purchaseFactory = new PurchaseOrderFactory;
$purchaseOrder = $purchaseFactory->instance();

Note: The later is probably even more useful/wise even after you've upgraded to PHP 5.4 because those two lines can be better separated and there is less code containing a hard-encoded classname, here the name of the factory class PurchaseOrderFactory. This will make you more fluent maintaining the code over time.

Even if he used his code on PHP 5.4 that would generate a syntax error.

PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR - Stack Ov...

php