I have to create a dynamic site map for a uni assignment using PHP.
I have saved the names of the links in a text file called "sitemap.txt". These names are the names of the pages minus their extensions and I am supposed to use this content to generate a link. The content looks like this:
Index,Services,Contact Us,Register,Login,Class Manager
My code is below:
<?php
$fp = fopen("sitemap.txt", "r");
echo '<p class="smallerText">';
while(!feof($fp))
{
$line = fgets($fp);
$array = explode(",", $line);
}
fclose($fp);
$num_elements = count($array);
$list = '<ul class="servicesList" name="sitemap">';
for($count = 0; $count < $num_elements; $count++)
{
$list .= "<li>$array[$count]</li>";
}
$list .= "</ul>";
echo "$list";
?>
So basically I have been able to print the contents of the file to the page without any issues. But I need to convert the static text into links.
Can anyone suggest a way? I was thinking using regex or string matching but I'm not sure how.
I am not sure what you are asking, but if it's creating link out of those names, can't you just ....
$YourDomain="http://mydomain.com/";
$ext=".php";
for($count = 0; $count < $num_elements; $count++)
{
$list .= "<li>$array[$count]</li>";
}
Related
Im trying to create a table of Jobs on my site, pulling info from an xml feed I have access to... I've looked at various examples online and videos but I can't seem to understand how it works. My xml feed returns the following node structure:
<OutputVacancyAsXml>
<Vacancy>
<VacancyID></VacancyID>
<Job></Job>
<ClosingDate></ClosingDate>
</Vacancy>
</OutputVacancyAsXml>
I've had success with pulling through one item with this code:
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
echo $x->Vacancy[5]->Job;
?>
But converting it to foreach seems to be where I'm struggling. Heres the code I have tried so far with no luck;
<?php
$html = "";
$url = "https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6";
$xml = simplexml_load_file($url);
for ($i = 0; $i < 10; $i++) {
$title = $xml->OutputVacancyAsXml->Vacancy[$i]->job;
$html .= "<p>$title</p>";
}
echo $html;
?>
Thanks all :)
Taken from the documentation
Note:
Properties ($movies->movie in previous example) are not arrays. They
are iterable and accessible objects.
With that kept in mind you can simple run over the nodes with foreach
$xml = simplexml_load_file($url);
foreach ($xml->OutputVacancyAsXml->Vacancy as $vacanacy)
{
echo (string)$vacanacy->Job; // Echo out the Job Title
}
Ok looks like I found a solution. Heres the code that worked for me plus it contains a little bit of code that pulls out duplicated (it was displaying each item 4 times!)...
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
$num = count($x->Vacancy);
//echo "num is $num";
$stopduplicates = array();
for ($i = 0; $i < $num; $i++) {
$job = $x->Vacancy[$i]->Job;
$closingdate = $x->Vacancy[$i]->ClosingDate;
// http://stackoverflow.com/questions/416548/forcing-a-simplexml-object-to-a-string-regardless-of-context
$vacancyid = (string) $x->Vacancy[$i]->VacancyID;
if (!in_array($vacancyid, $stopduplicates)) {
echo '
<tr class="job-row">
<td class="job-cell">'.$job.'</td>
<td class="date-cell">'.$closingdate.'</td>
<td class="apply-cell">
Apply Here
</td>
</tr>';
}
$stopduplicates[] = $vacancyid;
} //print_r($stopduplicates);
?>
I've written some code to read in data from a text file.
The data looks like this:
11:12:12:test titel 1
12:13:13:test titel 2
13:14:14:test titel 3
the following code reads the date, splits it one string for each line, those go in one array. This works perfectly.
After this, it should devide each line again in string that go in an array, and all these arrays go into one multidimensional array.
This last part doesnt work...
I think it's strange that instead of errors, of half the page, it shows just an empty page...
also, I've tried putting some of the code in comment, and so I've narrowed it down a bit. I give you guys the commented code, but all the comments should go away, and it should work like that!
thanks!
<?php
$filename = "data.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
$parts = null;
fclose($fp);
print_r($lines);
echo sizeof($lines);
for ($i=0; $i < sizeof($lines)-1 ; $i++) { //the minus 1 corrects the empty line automatically added when saving the data.txt file
//$tempParts[] = explode(":", $lines[i]);
//array_push($parts, $tempParts);
}
//echo "<br/>"
echo "all parts: "
//for ($row=0; $row < sizeof($lines)-1; $row++) {
// for ($col=0; $col < sizeof($parts[$row]); $col++) {
//echo $parts[$row][$col];
// }
//}
?>
I think preg_split will do what you want.
$filename = "data.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
//$content = "11:12:12:test titel 1
12:13:13:test titel 2
13:14:14:test titel 3";
$arr = preg_split("/(:|\n)/" ,$content);
var_dump($arr);
See here: http://www.phpliveregex.com/p/hNH
Click on preg_split on the right side of the screen to make it work
Maybe this works better for you?
preg_match_all("/(\d+):(\d+):(\d+):(.*)/", $content, $arr);
Click preg_match_all:
http://www.phpliveregex.com/p/hNW
I'm not sure to understand exactly what you want but you can try this :
if (!$fp = fopen("data.txt","r")) {
die("fail to open");
}else {
$all = array();
$row = 1;
while(!feof($fp)) { // foreach line
$ligne = fgets($fp,255); // get line content
$cols = explode(':', $line); // gets cols
$all[$row++] = $cols; // put cols on current row
}
var_dump($all); // dump all data stored by row
fclose($fp);
}
I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.
The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml
This is the code that im using:
$xml= "http://www.voetbalzone.nl/rss/rss.xml"
for ($i = 0; $i < 10; $i++)
{
$title = $xml->rss->channel->item[$i]->title;
}
The error I get: Premature end of data in tag
It works for me like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
for ($i = 0; $i < 10; $i++)
{
$title = $xml->channel->item[$i]->title;
}
?>
Note that you are overwriting the variable $title each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]
To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
$item_array = $xml->xpath("//rss/channel/item");
foreach($item_array as $item) {
echo $item->title . "\n";
}
?>
I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php
I have a file called "data.txt". In this file, there are 30 lines with strings who all have the same length. It looks like this:
2QA4ZRDUT
IDVLTLZSC
4GYC3HCMV
1W6409JD5
70P7U66TE
... and so on.
What I want to do now is reformatting these lines. I want 5 strings on one line, seperated with a ";". After that, I want a new line and the next 5. In the end, it should look like this:
2QA4ZRDUT;IDVLTLZSC;4GYC3HCMV;1W6409JD5;70P7U66TE;
NGN1TGF6G;JWVI7LSIZ;U99TMVXXK;KLBDMRPQV;MEFKLUO3;
... and so on, until the whole content of the file is reformatted like this.
The last hours I've been trying to accomplish this by using for, foreach and while loops but I only manage to get one line. I hope somebody can help me since I'm not that experienced with PHP.
This is the content of "data.txt":
2QA4ZRDUT
IDVLTLZSC
4GYC3HCMV
1W6409JD5
70P7U66TE
OG2JBBZF6
5391PHOVW
ZAJ3OZ4H2
GMOB9E9X7
Q8U4C8ZK1
0WDZLRWWJ
N487W3S24
PKXQFFEK3
NSMKC29IB
HOLI1T2ZB
DVPIVLLLS
FH7RSZWTM
9VSUWPZEX
NM6ZWV19I
NGN1TGF6G
JWVI7LSIZ
U99TMVXXK
KLBDMRPQV
MEFKLUO3L
LICFIK24W
ELGPLCK51
QQS4SOJV1
KJ2UVTU1B
FLQ6T7LG6
QJZLAPYN1
something like that:
<?php
$oldf=fopen('data.txt','r');
$newf=fopen('data_new.txt','w');
$i=0;
while(!feof($oldf))
{
$i++;
$line=fgets($oldf);
fwrite($newf,$line.';');
if($i%5==0)
fwrite($newf,"\n");
}
fclose($oldf);
fclose($newf);
Try this:
<?php
$file_name = "test.txt"; // Change file name as needed
$file_data = file_get_contents($file_name);
$file_data_array = explode('\n', $file_data);
$i = 0;
$new_file_data = "";
foreach ($file_data_array as $piece)
{
$new_file_data .= $piece . ';';
$i++;
if ($i % 5 == 0) // Change number of pieces you want on a line as needed
{
$new_file_data .= '\n';
}
}
file_put_contents($file_name, $new_file_data);
?>
This should do it:
$str = '2QA4ZRDUT
IDVLTLZSC
4GYC3HCMV
1W6409JD5
70P7U66TE
OG2JBBZF6
5391PHOVW
ZAJ3OZ4H2
GMOB9E9X7
Q8U4C8ZK1
0WDZLRWWJ
N487W3S24
PKXQFFEK3
NSMKC29IB
HOLI1T2ZB
DVPIVLLLS
FH7RSZWTM
9VSUWPZEX
NM6ZWV19I
NGN1TGF6G
JWVI7LSIZ
U99TMVXXK
KLBDMRPQV
MEFKLUO3L
LICFIK24W
ELGPLCK51
QQS4SOJV1
KJ2UVTU1B
FLQ6T7LG6
QJZLAPYN1';
$arr = explode(PHP_EOL, $str);
$c = 1;
$str3 = '';
foreach ($arr as $item) {
$str3.= $item.';';
if ($c % 5 == 0) {
$str3.= PHP_EOL;
}
++$c;
}
echo "<p>$str3</p>";
Be aware that you won't see the line breaks in the browser, but they will be apparent when you 'view source'.
Edit: I'm using PHP_EOL here instead of "\n" as the other answers have suggested. This ensures that the script will work correctly on any platform (Win, MacOS & *nix) as long as the PHP setting auto_detect_line_endings is set to true
I'm trying to parse the resources contained in a resources.arsc file as discussed in this question. I know the androidmanifest.xml file identifies resources located in the .arsc file. I have successfully managed to parse the header of the .arsc file, I can't figure out how to parse the resources themselves.
Can somebody please help me figure out how to parse the resources contained in an .arsc file?
My parsing code so far:
<?php
$doc = fopen('resources.arsc', 'r+');
for($i=1;$i<10;$i++){
$res[$i] = _unpack('V', fread($doc, 4));
}
for ($i = 0, $j = $res[6]; $i <= $j; $i++) {
$word = fread($doc, 4);
$stroffs[] = _unpack('V', $word);
}
$strings = array();
$curroffs = 0;
foreach($stroffs as $offs){
//read length
$len = _unpack('v', fread($doc, 2));
//read string
if($len>0){
$str = fread($doc, $len*2);
}else{
$str = '';
}
//null
$wd = fread($doc, 2);
//utf-16le
$strings[] = mb_convert_encoding($str, 'gbk', 'UTF-16LE');
//curr offset
$curroffs += ($len+1)*2 + 2;
}
$tpos = ftell($doc);
read_doc_past_sentinel($doc);
//fseek($doc, $tpos + $tpos % 4);
$i = 0;
$xmls = $strings;
print_r($xmls);
//------------------------------------
//and then...somebody konw format or continue parse?
//------------------------------------
function read_doc_past_sentinel(&$doc){
$pos = ftell($doc);
$count= 0;
while($word = fread($doc, 4)){
if(_unpack('V', $word)==-1)break;
}
$n = 1;
if ($count < $n){
while($word = peek_doc($doc, 4)){
if(_unpack('V', $word) != -1)break;
fread($doc, 4);
$n++;
if(isset($count) && $count >= $n)break;
}
echo 'skip '.$n.' chars<br />';
}
}
function peek_doc(&$doc, $size){
$data = fread($doc, $size);
fseek($doc, ftell($doc)-$size);
return $data;
}
function _unpack($m, $b){
//if(!$b)return '';
$res = unpack($m, $b);
return $res[1];
}
?>
This is a fairly complicated binary file. You will need way more code than that to parse it. :)
My suggestion would be to use the same code that the platform does -- that is the ResTable and related classes found here:
frameworks/base/include/utils/ResourceTypes.h
frameworks/base/libs/utils/ResourceTypes.cpp
Note that ResourceTypes.h also has definitions for the complete structure of the resource table (which the classes there use to parse it).
You may also just be able to use the aapt tool. This has a number of options for parsing resource-related data out of an .apk:
aapt d[ump] [--values] WHAT file.{apk} [asset [asset ...]]
badging Print the label and icon for the app declared in APK.
permissions Print the permissions from the APK.
resources Print the resource table from the APK.
configurations Print the configurations in the APK.
xmltree Print the compiled xmls in the given assets.
xmlstrings Print the strings of the given compiled xml assets.
If there is some other data you want not available with those commands, consider modifying the tool code in frameworks/base/tools/aapt to add stuff to parse out what you want. This tool is using ResTable to parse the resources.