php add class to Link in UL LI of current page - php

trying with regex at php to add a "curPage" class to my ul-li menu before sending to browser.
code:
$loadedMenu=preg_replace("/a href=\"" . $file . ".htm\"/", "a href=\"" . $file . ".htm\" class=\"curPage\"", $loadedMenu);
content of $loadedMenu:
<nav><ul><li rel="file" id="516054b57fbba">דף הבית</li><li rel="file" id="51681f81a440b">משתמש חדש</li><li rel="file" id="516054b57fb40">אודות</li><li rel="file" id="5160f37b822a3">דף חדש</li><li rel="folder" id="516054d162176">תיקייה חדשה<ul><li rel="file" id="516054b57fc62">מיטל הנסיכה שלי</li><li rel="file" id="516054b57fc9a">נסיון</li><li rel="file" id="516054b57fb82">עזרה</li></ul></li><li rel="folfil" id="5160552162177">תיקיית תוכן<ul><li rel="file" id="516054b57fbf2">test</li></ul></li><li rel="file" id="516054b57fc2a">גלידה</li><li rel="file" id="516054b57fcd2">נסיון0</li></ul></nav>

It is so much error prone to parse HTML text like you're doing that it is almost no-no.
Better to use DOM parser to parse and modify HTML like this code:
$file = 'foo.htm'; // set your value here
# fetch your HTML content here
$html = <<< EOF
<html>
Click link1 morestuff
Click www.example.com morestuff
notexample.com morestuff
Click link1
</html>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
// find all hrefs with $file in it
$nodelist = $xpath->query("//a[contains(#href, '" . $file . "')]");
// iterate thru found links
for($i=0; $i < $nodelist->length; $i++) {
$node = $nodelist->item($i);
# add class attribute to them
$node->setAttribute('class', 'curPage');
}
echo $doc->saveHTML();
OUTPUT:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>Click link1 morestuff
Click www.example.com morestuff
notexample.com morestuff
Click link1
</body></html>

Executing this code on the command line works fine. That part that does not work is hidden from us.
<?php
$file = "index";
$loadedMenu = 'whatever';
$loadedMenu=preg_replace("/a href=\"" . $file . ".htm\"/", "a href=\"" . $file . ".htm\" class=\"curPage\"", $loadedMenu);
echo $loadedMenu;
// whatever
?>
Perhaps the initial value of loadedMenu has single quotes instead of double, or a slightly different href value.
You could use a slightly more generic regex, capturing any filename instead of the specific file in your code...
$loadedMenu=preg_replace('/a href="(.+?)"/', 'a href="$1" class="curPage"', $loadedMenu);

Related

How to show image element from XML with php

I've tried what others have posted on stack overflow but it doesn't seem to work for me. So could anyone help please.
I have this xml document with a structure of:
<surveys>
<survey>
<section>
<page>
<reference>P1</reference>
<image><! [CDATA[<img src="imagepath">]]></image>
</page>
<page>
<reference>P2</reference>
<image><! [CDATA[<img src="imagepath">]]></image>
</page>
</section>
</survey>
</surveys>
Then this is my PHP code to get the image to show up:
function xml($survey){
$result = "<surveys></surveys>";
$xml_surveys = new SimpleXMLExtended($result);
$xml_survey = $xml_surveys->addChild('survey');
if ("" != $survey[id]){
$xml_survey_>addChildData($survey['image']);
}
This is my other file:
$image = “”;
if(“” != $image){
$image = <div class=“image_holder”> $image </div>
echo $image;
}
I'm not sure how to progress forward with this. so any help would be appreciated
It looks like you would like to fetch the image for a specific survey id. Well you can use DOM+Xpath To fetch this directly:
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$expression = 'string(
/surveys/survey/section/page[reference="P1"]/image
)';
$imageForSurvey = $xpath->evaluate($expression);
var_dump($imageForSurvey);
Output:
string(22) "<img src="imagepath1">"
The content of the CDATA section inside the image element is a separate HTML fragment. You can use it directly if you trust the source of the XML or you parse it as HTML.
$htmlFragment = new DOMDocument();
$htmlFragment->loadHTML($imageForSurvey);
$htmlXpath= new DOMXpath($htmlFragment);
var_dump(
$htmlXpath->evaluate('string(//img/#src)')
);
Output:
string(10) "imagepath"
Your example-logic is trying to create XML, not load it ;-)
First you need to find the path and/or address to the XML file, like:
$filePath = __DIR__ . '/my-file.xml';
Then load XML:
<?php
$filePath = __DIR__ . '/my-file.xml';
$document = simplexml_load_file($filePath);
$surveyCount = 0;
foreach($document->survey as $survey)
{
$surveyCount = $surveyCount + 1;
echo '<h1>Survey #' . $surveyCount . '</h1>';
foreach($survey->section->page as $page)
{
echo 'Page reference: ' . $page->reference . '<br>';
// Decode your image.
$imageHtml = $page->image;
$dom = new DOMDocument();
$dom->loadHTML($imageHtml);
$xpath= new DOMXpath($dom);
$image = $xpath->evaluate('string(//img/#src)');
if(!empty($image)) {
echo '<div class=“image_holder”>' . $image . '</div>';
}
echo "<br>";
}
}
?>
Note that you should replace <! [CDATA[ with <![CDATA[ (without space),
else you will get StartTag: invalid element name error probably.

What is the XPATH query to extract contents of a class from a div on a webpage in php?

I have written the following code but it just returns empty data :
enter code here
$code="CS225";
$url="https://cs.illinois.edu/courses/profile/{$code}";
echo $url;
$html = file_get_contents($url);
$pokemon_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$pokemon_doc->loadHTML($html);
libxml_clear_errors();
$pokemon_xpath = new DOMXPath($pokemon_doc);
$pokemon_row = $pokemon_xpath->query("//div[#id='extCoursesDescription']");
if($pokemon_row->length > 0){
foreach($pokemon_row as $row){
echo $row->nodeValue . "<br/>";
}
}
}
the website that i am trying to scrape is : https://cs.illinois.edu/courses/profile/CS225
The course content seems to be loaded on the source by the page on loading. But if you go through the source that is loaded you get to ...
<script type='text/javascript' src='//ws.engr.illinois.edu/courses/item.asp?n=3&course=CS225'></script>
From this you can track through to the url http://ws.engr.illinois.edu/courses/item.asp?n=3&course=CS225 and this gives you the actual content your after. So rather than the original URL, use this new one and you should be able to extract the information from there.
Although this content is all wrapped in document.write()'s.
Update:
To remove the document() bits - a simple way is to just process the content...
$html = file_get_contents($url);
$html = str_replace(["document.write('","');"], "", $html);
$html = str_replace('\"', '"', $html);

Append to start of href tag

I'm looking to turn
Some page to
Some page
using PHP. I'll have the HTML code of a random website so it's not as simple as using str_replace()
I've tried Replacing anchor href value with regex but that seems to just erase my entire page and I get a blank, white screen. Can anyone offer any help?
My code:
$html = file_get_contents(htmlentities($_GET['q'])); // Takes contents of website entered by user
$arr = array(); // Defines array
$html2 = ""; // Defines variable to write to later
$dom = new DOMDocument();
$dom->loadHTML($html); // Loads the HTML code displayed earlier
$domcss = $dom->getElementsByTagName('link');
foreach($domcss as $links) {
if( strtolower($links->getAttribute('rel')) == "stylesheet" ) {
$x = $links->getAttribute('href');
$html2 .= '<link rel="stylesheet" type="text/css" href="'.htmlentities($_GET['q']) . "/" . $x.'">';
}
} // This replaces all stylesheets from "./style.css", to "http://example.com/style.css"
echo $html2 . $html // Echos the entire webpage, with stylesheet links edited
To manipulate this with DOM, find the <a> tags and then if there is a href attribute, add the prefix in. The end of this code just echos out the resultant HTML...
$dom = new DOMDocument();
$dom->loadHTML($html); // Loads the HTML code displayed earlier
$aTags = $dom->getElementsByTagName('a');
$prefix = "http://example.com?q=";
foreach($aTags as $links) {
$href = $links->getAttribute('href');
if( !empty($href)) {
$links->setAttribute("href", $prefix.$href);
}
}
echo $dom->saveHTML();
$prefix contains the bit you want to add the the URL.

PHP DOM saveHTML changes formatting

I load external HTML page and with loadHTML.
Than I replace two childs and remove one.
saveHTML() method changes something and I do not want that.
It changes position of the closing
</head>
tag, puts it right after and on original page closing head is further down the line after few tags.
It also changes body tag to:
<body class="something">
to just
<body>
.
How I can save it using PHP DOM so it respects all the positioning and attributes?
Here is the code:
$document = new DOMDocument();
#$document->loadHTML($contents);
$login_signup = $document->getElementById('loginBar')->getElementsByTagName('div')->item(1);
$login_signup->removeChild($login_signup->getElementsByTagName('h3')->item(0));
$todays_a = $document->createElement('a', 'Todays Digest');
$todays_a->setAttribute('href', $domain . $digest_newsletter . date('mdy') . '.html');
$previous_a = $document->createElement('a', 'Previous Digest');
$previous_a->setAttribute('href', $domain . $digest_newsletter . date('mdy', strtotime('-1 day')) . '.html');
$todays_div = $document->getElementById('myDiv');
$todays_div->replaceChild($todays_a, $todays_div->getElementsByTagName('script')->item(0));
$previous_div = $document->getElementById('myDiv2');
$previous_div->replaceChild($previous_a, $previous_div->getElementsByTagName('script')->item(0));
$contents = $document->saveHTML($document);

php dom change nodeValue in anchor

I am trying to change NodeValue and save it to variable (or print it)
$html = '<html><body>
some a
some b
</body></html>';
libxml_use_internal_errors(true); // ignore malformed HTML
$xml = new DOMDocument();
$xml->loadHTML($html);
foreach($xml->getElementsByTagName('a') as $link) {
$link->nodeValue = $link->nodeValue . ' --- ' . $link->getAttribute('href');
}
print_r($html);
should print
<html><body>
some a --- a.html
some b --- b.html
</body></html>
but it won't. What am I doing wrong?
You're not actually changing $html, you are changing your DomDocument variable $xml. Instead of
print_r($html);
You need to:
echo $xml->saveHTML()

Categories