Skip to content

Instantly share code, notes, and snippets.

@wapcrazut
Created February 13, 2019 15:04
Show Gist options
  • Select an option

  • Save wapcrazut/0a87c7dffeab4a9656d6ccf186fc64be to your computer and use it in GitHub Desktop.

Select an option

Save wapcrazut/0a87c7dffeab4a9656d6ccf186fc64be to your computer and use it in GitHub Desktop.
Beautify var_dump
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Var dumper Formater</title>
<style>
body {
font-family: "Courier New", Courier, monospace;
padding-bottom:50px;
}
textarea {
width:100%;
height:250px;
}
.doIt {
width:100%;
height:35px;
margin-bottom:25px;
}
</style>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
(function($) {
// Source: http://www.bennadel.com/blog/142-ask-ben-javascript-string-replace-method.htm
// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
){
var strText = this;
var intIndexOfMatch = strText.indexOf( strTarget );
// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strText = strText.replace( strTarget, strSubString )
// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf( strTarget );
}
// Return the updated string with ALL the target strings
// replaced out with the new substring.
return( strText );
}
$('.doIt').click(function(){
var input = $('textarea').val();
var output = '';
var indent = 0;
// let's do some labour!
input = input.replaceAll('{ [', '{<br>[');
input = input.replaceAll('" ["', '"<br>["');
input = input.replaceAll('NULL [', 'NULL <br>[');
input = input.replaceAll('} ["', '}<br>["');
input = input.replaceAll('} }', '}<br>}');
input = input.replaceAll(') [', ')<br>[');
for (var i = 0, len = input.length; i < len; i++) {
output = output + input[i];
}
$('.output').html(input);
})
})(jQuery)
</script>
</head>
<body>
Real crappy PHP var_dump beautifier
<!-- will put some linebreaks for non-xdebug var_dumps -->
<!-- based on inferior logic and methods -->
<!-- Todo: indentation (but likely never to happen due to laziness) -->
<textarea placeholder='object(Mage_Sales_Model_Order)#119 (29) { ["_eventPrefix":protected]=> string(11) "sales_order" ["_eventObject":protected]=> string(5) "order" ["_addresses":protected]=> NULL ["_items":protected]=> NULL ["_payments":protected]=> NULL ["_statusHistory":protected]=> NULL ["_invoices":protected]=> NULL ["_tracks":protected]=> NULL ["_shipments":protected]=> NULL ["_creditmemos":protected]=> NULL ["_relatedObjects":protected]=> array(0) { } ["_orderCurrency":protected]=> NULL ["_baseCurrency":protected]=> NULL ["_actionFlag":protected]=> array(0) { } ["_canSendNewEmailFlag":protected]=> bool(true) ["_historyEntityName":protected]=> string(5) "order" ["_resourceName":protected]=> string(11) "sales/order" ["_resource":protected]=> NULL ["_resourceCollectionName":protected]=> string(22) "sales/order_collection" ["_cacheTag":protected]=> bool(false) ["_dataSaveAllowed":protected]=> bool(true) ["_isObjectNew":protected]=> NULL ["_data":protected]=> array(140) { ["entity_id"]=> string(4) "1234" ["state"]=> string(8) "complete" ["status"]=> string(8) "complete" ["coupon_code"]=> NULL ["protect_code"]=> string(6) "e64c0a" ["shipping_description"]=> NULL ["is_virtual"]=> string(1) "1" ["store_id"]=> string(1) "1" ["customer_id"]=> string(4) "9999" ["base_discount_amount"]=> string(6) "0.0000" ["base_discount_canceled"]=> NULL ["base_discount_invoiced"]=> string(6) "0.0000" ["base_discount_refunded"]=> NULL ["base_grand_total"]=> string(9) "999.0000" ["base_shipping_amount"]=> string(6) "0.0000"'></textarea>
<button class="doIt">Do It</button>
<div class="output">
// SAMPLE OUTPUT<br>
object(Mage_Sales_Model_Order)#119 (29) {<br>["_eventPrefix":protected]=&gt; string(11) "sales_order"<br>["_eventObject":protected]=&gt; string(5) "order"<br>["_addresses":protected]=&gt; NULL <br>["_items":protected]=&gt; NULL <br>["_payments":protected]=&gt; NULL <br>["_statusHistory":protected]=&gt; NULL <br>["_invoices":protected]=&gt; NULL <br>["_tracks":protected]=&gt; NULL <br>["_shipments":protected]=&gt; NULL <br>["_creditmemos":protected]=&gt; NULL <br>["_relatedObjects":protected]=&gt; array(0) { }<br>["_orderCurrency":protected]=&gt; NULL <br>["_baseCurrency":protected]=&gt; NULL <br>["_actionFlag":protected]=&gt; array(0) { }<br>["_canSendNewEmailFlag":protected]=&gt; bool(true)<br>["_historyEntityName":protected]=&gt; string(5) "order"<br>["_resourceName":protected]=&gt; string(11) "sales/order"<br>["_resource":protected]=&gt; NULL <br>["_resourceCollectionName":protected]=&gt; string(22) "sales/order_collection"<br>["_cacheTag":protected]=&gt; bool(false)<br>["_dataSaveAllowed":protected]=&gt; bool(true)<br>["_isObjectNew":protected]=&gt; NULL <br>["_data":protected]=&gt; array(140) {<br>["entity_id"]=&gt; string(4) "1234"<br>["state"]=&gt; string(8) "complete"<br>["status"]=&gt; string(8) "complete"<br>["coupon_code"]=&gt; NULL <br>["protect_code"]=&gt; string(6) "e64c0a"<br>["shipping_description"]=&gt; NULL <br>["is_virtual"]=&gt; string(1) "1"<br>["store_id"]=&gt; string(1) "1"<br>["customer_id"]=&gt; string(4) "9999"<br>["base_discount_amount"]=&gt; string(6) "0.0000"<br>["base_discount_canceled"]=&gt; NULL <br>["base_discount_invoiced"]=&gt; string(6) "0.0000"<br>["base_discount_refunded"]=&gt; NULL <br>["base_grand_total"]=&gt; string(9) "999.0000"<br>["base_shipping_amount"]=&gt; string(6) "0.0000"
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment