Need help sorting this logic with JSON Pretty Print - php

Trying to pretty print json, and works fine, cept, seems to add an extra line when printing out an array of objects between each object array. Could use some help on the logic of this as I haven't touched this in awhile...
code for pretty printing it is as follows:
public function pretty_print($json_data, $line_numbers = true)
{
$return = '';
$space = 0;
$flag = false;
$json_data = trim($json_data);
$line = 1;
if (!empty($json_data)) {
if (!empty($line_numbers))
$return .= '<div class="line" data-line-number="' . $line . '">';
//loop for iterating the full json data
for($counter = 0; $counter < strlen($json_data); $counter++)
{
//Checking ending second and third brackets
if ($json_data[$counter] == '}' || $json_data[$counter] == ']')
{
$space--;
$line++;
$return .= !empty($line_numbers) ? '</div><div class="line" data-line-number="' . $line . '">' : PHP_EOL;
$return .= str_repeat(' ', ($space*4));
}
//Checking for double quote(“) and comma (,)
if ($json_data[$counter] == '"' && ((!empty($counter) && $json_data[$counter-1] == ',') || ($counter > 1 && $json_data[$counter-2] == ',')))
{
$line++;
$return .= !empty($line_numbers) ? '</div><div class="line" data-line-number="' . $line . '">' : PHP_EOL;
$return .= str_repeat(' ', ($space*4));
}
if ($json_data[$counter] == '"' && !$flag)
{
if ( (!empty($counter) && $json_data[$counter-1] == ':') || ($counter > 1 && $json_data[$counter-2] == ':' ))
$return .= ' <span class="json-property">';
else
$return .= '<span class="json-value">';
}
$return .= $json_data[$counter];
//Checking conditions for adding closing span tag
if ($json_data[$counter] == '"' && $flag) {
$return .= '</span>';
}
if ($json_data[$counter] == '"')
$flag = !$flag;
//Checking starting second and third brackets
if ($json_data[$counter] == '{' || $json_data[$counter] == '[')
{
$space++;
$line++;
$return .= !empty($line_numbers) ? '</div><div class="line" data-line-number="' . $line . '">' : PHP_EOL;
$return .= str_repeat(' ', ($space*4));
}
}
if (!empty($line_numbers))
$return .= '</div>';
}
return !empty($return) ? trim($return) : json_encode(json_decode($json_data, true), JSON_PRETTY_PRINT);
}
But seems to parse the json with an extra <div class="line" data-line-number=""></div>
Here's an image of this and would like to get rid of the extra space between objects of the array in here if possible. Any help here, would really appreciate it.

Why the heck are you manually parsing JSON? That code is going to be incredibly difficult to reason about and maintain, especially if you come back to it later when a bug almost inevitably presents itself.
Rather than taking the difficult approach, consider instead doing the following:
1. Reformat the JSON so that it suits your needs. In this case, for example, you prefer to keep the closing and ending brackets of objects on the same line, rather than on separate lines.
2. Split the already-pretty-formatted JSON into separate lines.
3. Wrap the individual lines of your JSON in HTML.
4. Re-join the lines to get your final HTML.
function prettyWrapJson($json_data, $line_numbers = true) {
// Ensure that our JSON is in pretty format.
$json_data = json_encode(json_decode($json_data, true), JSON_PRETTY_PRINT);
// Modify the formatting so that adjacent closing and opening curly braces are on the same line.
// Note: we can add a similar line if we want to do the same for square brackets.
$json_data = preg_replace('/},\n +{/s', '},{', $json_data);
$line_number = 1;
// Coerce a boolean value.
$line_numbers = !empty($line_numbers);
// Split into an array of separate lines.
$json_lines = explode("\n", $json_data);
// Wrap the individual lines.
$json_lines = array_map(function($json_line) use ($line_numbers, &$line_number) {
// Check if this line contains a property name.
if(preg_match('/^( +"[^"]+"):/', $json_line, $matches)) {
// Similar result to explode(':', $json_line), but safer since the colon character may exist somewhere else in the line.
$parts = array($matches[1], substr($json_line, strlen($matches[1]) + 1));
// Wrap the property in a span, but keep the spaces outside of it.
$parts[0] = preg_replace('/^( +)/', '$1<span class="json-property">', $parts[0]) . '</span>';
// We only want to wrap the other part of the string if it's a value, not an opening brace.
if(strpos($parts[1], '{') === false && strpos($parts[1], '[') === false) {
// Similarly, wrap the value in a span, but keep the spaces outside of it.
$parts[1] = preg_replace('/^( +)/', '$1<span class="json-value">', $parts[1]) . '</span>';
}
// Re-join the string parts with the colon we stripped out earlier.
$json_line = implode(':', $parts);
}
// Finally, we can wrap the line with a line number div if needed.
if($line_numbers) {
$json_line = '<div class="line" data-line-number="' . ($line_number++) . '">' . $json_line . '</div>';
}
return $json_line;
}, $json_lines);
// Re-join the lines and return the result.
return implode("\n", $json_lines);
}
You may need to tinker with it slightly to get it formatted exactly to your preferences, but this should be much easier for you to work with.

Related

PHP prevent extra empty line when writing text to file

I'm having a small issue regarding a foreach() loop and writing an array to a text file within.
The loop gives me the format in the file that I want but it also adds (as I was told) an unwanted empty line at the end of the file.
Here is my piece of code:
foreach($data_arr as $data => $input)
{ fwrite($fh, $data . ":" . $input . "\n") or die("something went wrong here"); }
Is there a way to prevent this from happening and not add the \n when it reaches the end of the array?
You can do something like this :
$data_array = ['c', 'h', "hf"];
$last = count($data_array) - 1; #size of the array
foreach($data_array as $data => $input)
{
$separator = $data == $last ? "" : "\n"; #if is last, then seperator isn't a back to line
fwrite($fh, $data . ":" . $input . $separator);
}
Just check if current index is the last one, then eventually add "\n" to the string
$last_index = count($data_arr)-1;
foreach ($data_arr as $data => $input)
{
$string = $data . ":" . $input;
if ($data != $last_index)
{
$string .= "\n";
}
fwrite($fh, $string) or die("something went wrong here");
}
Determining whether you are doing something for the last time, is cumbersome - for starters, you need to count your items, and then you need a loop index to compare to that count …
Much easier to determine, whether you are doing something for the first time. So just reverse your logic here - do not try to output “all lines, followed by a newline each, except the last one”, but instead, output “all lines preceded by a newline each, except the first one.”
You could use a simple boolean flag for that:
$is_first_line = true;
foreach($data_arr as $data => $input) {
fwrite($fh, ($is_first_line ? "" : "\n") . $data . ":" . $input);
$is_first_line = false;
}
Or you just append a variable before the line data all the time - and simply make that variable “empty” on the first iteration, and then fill it with a newline character for all the following ones:
$prefix = "";
foreach($data_arr as $data => $input) {
fwrite($fh, $prefix . $data . ":" . $input);
$prefix = "\n";
}

dont break line if data ending "\" while writing in csv in php

writing in csv file from database, if same id trying to append in "comments" coulmn with same row. but if from database value ending with "\" sign so next comments is writing in new line..if value is not ending with "\" for this working properly.
function cleanComments($string) {
$cleanstring = trim($string);
$returnstring = preg_replace("/\n/", "", $cleanstring);
return $returnstring;
}
function remove_special_char($string) {
$RemoveChars[] = "([,%+*¢£¥¤¡~©¦§¨ª«¬®¯°±²³´µ¶¸¹º»¿\[:^print:]])";
$ReplaceWith = "";
$text = preg_replace($RemoveChars, $ReplaceWith, $string);
return $text;
}
$comment_text = cleanComments($query_result['user_name'] . ':' . $query_result['comment']);
if ($counter == 1) {
$row['comment'].= remove_special_char($comment_text);
} else {
$row['comment'].= PHP_EOL . remove_special_char($comment_text);
}
if "\" this special character is ending with dont write in line in csv check my code anyting i missed? or any solution ?
i changed to this.
$comment_text = cleanComments(stripslashes($query_result['user_name'] . ':' . $query_result['comment']));
if ($counter == 1) {
$row['comment'].= remove_special_char($comment_text);
} else {
$row['comment'].= PHP_EOL . remove_special_char($comment_text);
}
its working properly now.

edit display of php recordSet output

The following code produces each result as text. I would like to add some html to each result, to change it from regular text to an href tag:
while (!$recordSet->EOF()) {
if ($pclass_name_list == '') {
$pclass_name_list .= $recordSet->fields['class_name'];
} else {
$pclass_name_list .= ',' . $recordSet->fields['class_name'];
}
$recordSet->MoveNext();
}
The above produces Result 1, Result 2. I'd like to change these to
Result 1
Result 2
etc..
If you are doing a simple addition of content into a string, you can either wrap the variable in curly braces and include it in a string that has double quotes (as below), or you can use the sprintf function to merge content into a template.
$pclass_name_list = array();
while (!$recordSet->EOF()) {
$current_class_name = $recordSet->fields['class_name'];
$pclass_name_list[] = "{$current_class_name}";
$recordSet->MoveNext();
}
$pclass_name_list = implode(", ", $pclass_name_list);
Maybe:
if ($pclass_name_list == '') {
$pclass_name_list .= '' . $recordSet->fields['class_name'] . '';
} else {
$pclass_name_list .= ', ' . $recordSet->fields['class_name'] . '';
}

Bug in custom code for indented XML output in PHP

I have this code for automatically indenting XML through PHP:
function xmlpp($xml, $html_output=false) {
if ($xml == '') return 'NULL';
try {
$xml_obj = #new SimpleXMLElement($xml);
} catch (Exception $ex) {
// Error parsing xml, return same string
return ($html_output) ? htmlentities($xml) : $xml;
}
$level = 4;
$indent = 0; // current indentation level
$pretty = array();
// get an array containing each XML element
$xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
// shift off opening XML tag if present
if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
//$pretty[] = array_shift($xml);
array_shift($xml);
}
foreach ($xml as $el) {
if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
// opening tag, increase indent
$pretty[] = str_repeat(' ', $indent) . $el;
$indent += $level;
} else {
if (preg_match('/^<\/.+>$/', $el)) {
$indent -= $level; // closing tag, decrease indent
}
if ($indent < 0) {
$indent += $level;
}
$pretty[] = str_repeat(' ', $indent) . $el;
}
}
$xml = implode("\n", str_replace('"', "'", $pretty));
return ($html_output) ? htmlentities($xml, ENT_COMPAT, 'UTF-8') : $xml;
}
The issue is that whenever I get an attribute value containing a / character, the indentation level is reduced. For example, the output produced for the following is incorrect:
<function desc='Cancel/Refund'>
<const value='1'/>
<const value='1'/>
<const value='1'/>
</function>
I know the regular expression shouldn't match the words Cancel/Refud but it does and I can't figure out how to fix this.
Any hints would be appreciated.
<[^\/].+[^\/]>
The [^\/] at the beginning and end of the regex says to match tags that don't start with a / and don't end with a /. This way you only get opening tags and not closing tags or empty tags. The .+ will match anything so it doesn't matter if you have / inside the tags attributes or not as long as it doesn't start or end with a /.

Need some help with XML parsing

The XML feed is located at: http://xml.betclick.com/odds_fr.xml
I need a php loop to echo the name of the match, the hour, and the bets options and the odds links.
The function will select and display ONLY the matchs of the day with streaming="1" and the bets type "Ftb_Mr3".
I'm new to xpath and simplexml.
Thanks in advance.
So far I have:
<?php
$xml_str = file_get_contents("http://xml.betclick.com/odds_fr.xml");
$xml = simplexml_load_string($xml_str);
// need xpath magic
$xml->xpath();
// display
?>
Xpath is pretty simple once you get the hang of it
you basically want to get every match tag with a certain attribute
//match[#streaming=1]
will work pefectly, it gets every match tag from underneath the parent tag with the attribute streaming equal to 1
And i just realised you also want matches with a bets type of "Ftb_Mr3"
//match[#streaming=1]/bets/bet[#code="Ftb_Mr3"]
This will return the bet node though, we want the match, which we know is the grandparent
//match[#streaming=1]/bets/bet[#code="Ftb_Mr3"]/../..
the two dots work like they do in file paths, and gets the match.
now to work this into your sample just change the final bit to
// need xpath magic
$nodes = $xml->xpath('//match[#streaming=1]/bets/bet[#code="Ftb_Mr3"]/../..');
foreach($nodes as $node) {
echo $node['name'].'<br/>';
}
to print all the match names.
I don't know how to work xpath really, but if you want to 'loop it', this should get you started:
<?php
$xml = simplexml_load_file("odds_fr.xml");
foreach ($xml->children() as $child)
{
foreach ($child->children() as $child2)
{
foreach ($child2->children() as $child3)
{
foreach($child3->attributes() as $a => $b)
{
echo $a,'="',$b,"\"</br>";
}
}
}
}
?>
That gets you to the 'match' tag which has the 'streaming' attribute. I don't really know what 'matches of the day' are, either, but...
It's basically right out of the w3c reference:
http://www.w3schools.com/PHP/php_ref_simplexml.asp
I am using this on a project. Scraping Beclic odds with:
<?php
$match_csv = fopen('matches.csv', 'w');
$bet_csv = fopen('bets.csv', 'w');
$xml = simplexml_load_file('http://xml.cdn.betclic.com/odds_en.xml');
$bookmaker = 'Betclick';
foreach ($xml as $sport) {
$sport_name = $sport->attributes()->name;
foreach ($sport as $event) {
$event_name = $event->attributes()->name;
foreach ($event as $match) {
$match_name = $match->attributes()->name;
$match_id = $match->attributes()->id;
$match_start_date_str = str_replace('T', ' ', $match->attributes()->start_date);
$match_start_date = strtotime($match_start_date_str);
if (!empty($match->attributes()->live_id)) {
$match_is_live = 1;
} else {
$match_is_live = 0;
}
if ($match->attributes()->streaming == 1) {
$match_is_running = 1;
} else {
$match_is_running = 0;
}
$match_row = $match_id . ',' . $bookmaker . ',' . $sport_name . ',' . $event_name . ',' . $match_name . ',' . $match_start_date . ',' . $match_is_live . ',' . $match_is_running;
fputcsv($match_csv, explode(',', $match_row));
foreach ($match as $bets) {
foreach ($bets as $bet) {
$bet_name = $bet->attributes()->name;
foreach ($bet as $choice) {
// team numbers are surrounded by %, we strip them
$choice_name = str_replace('%', '', $choice->attributes()->name);
// get the float value of odss
$odd = (float)$choice->attributes()->odd;
// concat the row to be put to csv file
$bet_row = $match_id . ',' . $bet_name . ',' . $choice_name . ',' . $odd;
fputcsv($bet_csv, explode(',', $bet_row));
}
}
}
}
}
}
fclose($match_csv);
fclose($bet_csv);
?>
Then loading the csv files into mysql. Running it once a minute, works great so far.

Categories