Text:
TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class
What I have so far: (Inside $display->getExtraHTML() is the text). Could someone guide me towards what I need to do to adapt my code to get the results I want.
<?php
$additionalHTML = explode("\n", $display->getExtraHTML());
$html = "";
$html .= "<ul>";
foreach($additionalHTML as $key => $item){
$html .= "<li>$item</li>";
}
$html .= "</ul>";
echo $html;
?>
I know I can use something like this to get string between, but how do I use it to get all the values i need?
$string = strstr($display->getExtraHTML(), "HT-"); // gets all text from HT
$string = strstr($string, "CLASS-", true); // gets all text before CLASS
Can I use both explode and strstr to get to where I want?
Expect HTML markup:
Expected Result: (Get the values from HT- and CLASS-)
<ul>
<li>Child1 Class1</li>
<li>Child2 Class2</li>
<li>Child3 Class3</li>
<li>Child4 Class4</li>
</ul>
Complete solution with preg_match_all function:
$txt = '
TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class';
preg_match_all('/^HT-(\S+)\s+CLASS-(\S+)/m', $txt, $m);
$html = "<ul>";
if (isset($m[1]) && isset($m[2])){
foreach(array_map(null, $m[1], $m[2]) as $pair){
$html .= "<li>". implode(' ', $pair) ."</li>";
}
}
$html .= "</ul>";
echo $html;
The output (push Run code snippet):
<ul><li>Child1 Class1</li><li>Child2 Class2</li><li>Child3 Class3</li><li>Child4 Class4</li></ul>
Here's the solution
<?php
$str = "TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class";
$additionalHTML = explode("\n", $str);
$html = "";
$html .= "<ul>";
foreach($additionalHTML as $key => $item){
if(substr($item,0,3) == "HT-") {
$i = explode(" ",$item);
$a = substr($i[0],3);
$b = substr($i[1],6);
$html .= "<li>$a"." ". "$b</li>";
}
}
$html .= "</ul>";
echo $html;
Result
Child1 Class1
Child2 Class2
Child3 Class3
Child4 Class4
You may use regex, to find the matching lines and extract required data:
if(preg_match("/HT-([A-Za-z0-9]+) CLASS-([A-Za-z0-9]+)/", $item, $output))
{
$html .= "<li>".implode(" ",array_slice($output,1))."</li>";
}
i'm working on setting up a page that uses PHP to layout the page, but i also need this PHP HTML code inserted into the PHP page for a search function. The search is done on a different page, and then the action is sent to the results page. I'm trying to get the PHP and HTML to mix together. I've tried using echo to no success. Basically i need the PHP HTML code to put the results into the $layout->content("");
<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php');
$layout = new default_layout();
$layout->title('IT KB Search');
$layout->content("<div class='border'>");
$layout->content('<h1>IT Support Knowledge Base - Search Results</h1>');
if (isset($_GET['q'])) {
$query = rawurlencode( strip_tags($_GET['q']));
$timestamp = time();
$baseUrl = 'https://oursite.atlassian.net/wiki';
$url = $baseUrl.'/rest/api/content/search?cql=space=KB%20AND%20type=page%20AND%20title~'.$query;
// To enable authenticated search:
// $url .= "&os_username=$username&os_password=$password";
$response = file_get_contents($url);
$response = json_decode($response);
$results = $response->results;
Echo '<div>';
Echo ' <ol>';
foreach($results as $item) {
Echo ' <li><strong><a href="';
$baseUrl. $item-> _links-> webui
Echo ' " target='_blank'>';
$item->title
Echo ' </a></strong></li>';
}
Echo ' </ol></div><hr>';
}
$layout->content("</div>");
$layout->render();
?>
You can to store the HTML to a string and then pass it to the $layout->content() function, like this...
<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php');
$layout = new default_layout();
$layout->title('IT KB Search');
$layout->content("<div class='border'>");
$layout->content('<h1>IT Support Knowledge Base - Search Results</h1>');
if (isset($_GET['q'])) {
$query = rawurlencode( strip_tags($_GET['q']));
$timestamp = time();
$baseUrl = 'https://oursite.atlassian.net/wiki';
$url = $baseUrl.'/rest/api/content/search?cql=space=KB%20AND%20type=page%20AND%20title~'.$query;
// To enable authenticated search:
// $url .= "&os_username=$username&os_password=$password";
$response = file_get_contents($url);
$response = json_decode($response);
$results = $response->results;
# Change Starts
$html = '<div>';
$html .= '<ol>';
foreach($results as $item) {
$html .= '<li><strong><a href="';
$html .= $baseUrl. $item-> _links-> webui;
$html .= '" target="_blank">';
$html .= $item->title;
$html .= '</a></strong></li>';
}
$html .= '</ol></div><hr>';
$html .= '</div>';
$layout->content($html);
# Change Ends
}
$layout->content('</div>');
$layout->render();
{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}
You'll need to use json_decode to convert the json string to an object or associative array, you may then iterate over the result. Remember to use htmlspecialchars or htmlentities to escape the data.
This should help get you started:
function table_encode(array $array) {
$buffer = '<table border="1"><thead>';
foreach (array_keys($array) as $header) {
$buffer .= '<th style="text-align:left">' . htmlspecialchars($header) . '</th>';
}
$buffer .= '</thead><tbody><tr>';
foreach ($array as $value) {
$buffer .= '<td>';
if (is_array($value)) {
$buffer .= table_encode($value);
} else {
$buffer .= htmlspecialchars($value);
}
$buffer .= '</td>';
}
$buffer .= '</tbody></table>';
return $buffer;
}
$json = '{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}';
$json = json_decode($json, true);
$html = table_encode($json['data']);
echo $html;
$json = json_decode($array);//Decode Json
eccho($json);
function eccho($json){
foreach ($json as $file)
{
if(is_array($file))
return eccho($json);
else
echo "<tr>";
echo "<td> $file </td>";
echo "</tr>";
}
}
maybe this is useful for you...
you can't convert direct in to html..
use can use json_decode() function to convert it in to php array like follow or else use parse the json in jquery/javascript..
$var = '{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}';
$ab = json_decode($var,true);
print_r($ab); // here u will get your json data in php array. so now you can print it in html based on your requirements..
hope it will help you
If your get response form server through AJAX then use
var json = '{"status":"OK","data":{"train_number":"11111","chart_prepared":false,"pnr_number":"4259444444","train_name":"AAA","travel_date":{"timestamp":1394841600,"date":"10-4-2014"},"from":{"code":"ABC","name":"CITYJUNCTION","time":"20:30"},"to":{"code":"XYZ","name":"TTR","time":"05:35"},"alight":{"code":"DDD","name":"TTR","time":"05:35"},"board":{"code":"ABC","name":"FFF","time":"20:30","timestamp":1394895600},"class":"SL","passenger":[{"seat_number":"S7 , 11,GN","status":"CNF"},{"seat_number":"S7 , 06,GN","status":"CNF"}]}}';
obj = JSON.parse(json);
or use
data = eval( '(' + json + ')');
Now you can access
var status = data.status;
and you can easily display display data in html.
I have a class that is building some HTML using data stored in an array. There are around 100 items in this array. Each item includes information like company name, a description, and flags for the different programming languages the company supports. I am doing string concatenation as I build the HTML for each item.
I have noticed that performance suddenly takes a huge hit when I append the programming language data. I see the page rendering timer jump from 0.15 secs to ~0.60 secs. This time includes grabbing the same data from the database each time. I can consistently get the performance to jump between these 2 times but commenting/uncommenting the following line of code:
$html .= '<div class="programmingLanguages"><strong>Programming Languages</strong> '.implode(', ', $progLanguagesArray).'</div>';
I've also been able to get the same performance drop by appending a long test string, something like this:
$html .= 'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest';
What's especially bizarre is that I have another line of code that uses the same 'implode' function and does NOT make any significant difference in performance:
$html .= '<div class="integrationMethods"><strong>Integration Methods:</strong> '.implode(', ', $intMethodsArray).'</div>';
Does anybody have any insight into what might be going on here? I am doing tons of concatenation like this elsewhere in my code and haven't seen anything like this before. At this point, I'm stumped.
Here's the full class:
class DeveloperView {
public static function getHtml($developers) {
$html = '';
$html .= '<div>';
$html .= '<div>';
$count = 0;
foreach ($developers as $developer) {
$url = $developer['attributes']['url'];
$phone = $developer['attributes']['phone'];
$company = $developer['attributes']['desc'];
$active = $developer['attributes']['active'];
$desc = $developer['object_value'];
$intMethodsArray = array();
if ($developer['attributes']['m1']) { $intMethodsArray[] = 'method 1'; }
if ($developer['attributes']['m2']) { $intMethodsArray[] = 'method 2'; }
if ($developer['attributes']['m3']) { $intMethodsArray[] = 'method 3'; }
if ($developer['attributes']['m4']) { $intMethodsArray[] = 'method 4'; }
if ($developer['attributes']['m5']) { $intMethodsArray[] = 'method 5'; }
$progLanguagesArray = array();
if ($developer['attributes']['dotnet']) { $progLanguagesArray[] = '.Net (C# or VB.Net)'; }
if ($developer['attributes']['asp']) { $progLanguagesArray[] = 'Classic ASP'; }
if ($developer['attributes']['cf']) { $progLanguagesArray[] = 'Cold Fusion'; }
if ($developer['attributes']['java']) { $progLanguagesArray[] = 'Java'; }
if ($developer['attributes']['php']) { $progLanguagesArray[] = 'PHP'; }
if ($developer['attributes']['perl']) { $progLanguagesArray[] = 'Perl'; }
if ($developer['attributes']['other']) { $progLanguagesArray[] = 'Other'; }
$html .= '<div class="';
if ($count % 2 == 0) {
$html .= 'listingalt';
} else {
$html .= 'listing';
}
$html .= '">';
$html .= '<div class="developerPhone">'.$phone.'</div>';
$html .= '<a class="ext_link" target="_blank" href="'.$url.'">'.$company.'</a>';
$html .= '<div>';
if (!empty($intMethodsArray)) {
$html .= '<div class="integrationMethods"><strong>Integration Methods:</strong> '.implode(', ', $intMethodsArray).'</div>';
}
if (!empty($progLanguagesArray)) {
$html .= '<div class="programmingLanguages"><strong>Programming Languages</strong> '.implode(', ', $progLanguagesArray).'</div>';
}
$html .= '</div>';
$html .= '<p>'.$desc.'</p>';
$html .= '</div>'."\n";
$count++;
}
$html .= '</div></div>';
return $html;
}
}
Now that I can provide an answer, I'll just post my follow-up comment as the 'answer'...
I did indeed have a 'bug' in my timer, in that it was calculating the end processing time AFTER the echo of the HTML. So the amount of data being sent to the browser was effecting the processing time, where I was expecting to see the time spent processing BEFORE transmitting any data.
This is sort of confusing to explain, so thank you ahead of time for bearing with me.
I am using Kohana PHP framework to develop an application. I have a model function that accepts parameters for a search, and should return an XML styled page. I need this to be read by the controller with SimpleXML. Any ideas how to do this?
$o = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$o .= "<feed>\n";
$o .= "\t<search_phrase>$q</search_phrase>\n";
if(isset($entries)){
uasort($entries, 'compare_weight');
/**
* Build the xml data
*/
foreach($modules as $module){
$o .= "\t<search_location>$module</search_location>\n";
}
foreach($entries as $k=>$entry){
$o .= "\n\t<entry>\n";
$o .= "\t\t<title>$entry[title]</title>\n";
$o .= "\t\t<url>$entry[url]</url>\n";
$o .= "\t\t<weight>$entry[weight]</weight>\n";
$o .= "\t\t<module>$entry[module]</module>\n";
if($entry['owners']){
foreach($entry['owners'] as $owner){
$o .= "\t\t<owners>\n";
$o .= "\t\t\t<owner_id>$owner[owner_id]</owner_id>\n";
$o .= "\t\t\t<owner_name>$owner[owner_name]</owner_name>\n";
$o .= "\t\t\t<profile_link>$owner[profile_link]</profile_link>\n";
$o .= "\t\t</owners>\n";
}
}
$o .= "\t</entry>\n";
}
}else{
$o .= "\t<noresult>true</noresult>\n";
}
$o .= "</feed>\n";
return $o;
The controller functions like this... It's the closest I'm able to come to wrapping my head how to do this.
$return= $this->search->search($_GET);
$xml = new SimpleXMLElement($return);
die($xml);
It returns a blank document with 44 blank lines. Any direction would be helpful.
insted of using the constructor of simpleXML try using the simplexml_load_string() function
Try changing the return of your $xml feed to:
return echo $o;