I want to fetch the <a> link within the div glance_details. I can't really make it work. Don't worry about including and the url and things, that is all correct.
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details') as $element3) {
$html3->find('a',0)->outertext;
}
with
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details') as $element3) {
$knaoss = $element3->plaintext;
echo $knaoss;
}
I can fetch the plain text content of the div, but what I want is the anchor (a) that will be within the div.
This is similar to what I receive in $knaoss if I remove the ->plaintext:
<div class="glance_details">
<a href="http://www.example.com/">
<img src="http://www.example.com/img.png">
</a>
"This is a description of the example"
</div>
Though all I want from it is:
http://www.example.com/
I must delete this answer because do not match the OP requirements after he has posted the requested HTML code
The solution was simple. Only had to change:
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details') as $element3) {
$knaoss = $element3->plaintext;
}
to
$redirect = $url;
$html3 = file_get_html($redirect);
foreach($html3->find('div.glance_details > a') as $element3) {
$knaoss = $element3->href;
}
to find the href within div.glance_details. Problem was I used words like "url" and "link" instead of href, and could therefor not make it work.
Related
The posts in my site has video(single) from anyone of the following embeds.
Youtube
Facebook
Instagram
My question is while fetching them on front end I want to findo out whether my content has an embed, if so which of the following is embedded. (iframe presence checking is one (dirty)way still it own work for instagram)
PHPCODE:
$video_start = strpos($singlePost->post_content, "<iframe");//Get to the start of the iframe(video)
$video_stop = strpos($singlePost->post_content, "</iframe>");//Get to the end of the iframe(video)
$iframe_content = substr($singlePost->post_content, $video_start, $video_stop);
$xpath = new DOMXPath(#DOMDocument::loadHTML($iframe_content));
$iframe_src = $xpath->evaluate("string(//iframe/#src)");
$parsed_url = parse_url($iframe_src);
$host = $parsed_url['host'];
if(strpos($host, "youtube") !== false) { // If it is a youtube video append this
$iframe_src = $iframe_src."?rel=0";// This option has to be appended of youtube URL's
$related_social_icon = "youtube";
$related_social_media = "youtube";
}
<iframe class="<?php echo $iframe_class; ?>" src="<?php echo $iframe_src; ?>" style="background-size: cover;" allowfullscreen></iframe>
Above code works fine for youtube, but does not work for instagram coz when inserting instagram comes as blockquote tags,but if you echo them it will be straight away become iframe tags due to the script in it.
I would go for something like this:
add_filter('the_content', function($content) {
$identifier = '<embed';
if (strpos($content, $identifier) !== false) {
// identifier found
$content = '<h1>This page includes an embed</h1>'.$content;
}
return $content;
});
I'm not sure how your embeds look like, you are talking about iframes to. So you need to find some identifiers that you can check.
Your post probably got downvoted because it could have some more information?
We load html by url. After that creating DOMDocument
libxml_use_internal_errors(true); // disable errors
$oHtml = new DOMDocument();
if (!$oHtml->loadHTML($this->getHtml($aData['href']))) {
return false;
}
Next step is to delete fancybox or other popUp links... In our case image code is
<a onclick="return hs.expand(this)" href="http://domain.com/uploads/09072014106.jpg">
<img title="Some title" alt="Some title" src="http://domain.com/uploads/thumbs/09072014106.jpg">
</a>
And we execute our method for it ...
$this->clearPopUpLink($oHtml); // delete parent <a tag....
Method...
private function clearPopUpLink($oHtml)
{
$aLink = $oHtml->getElementsByTagName('a');
if (!$aLink->length) {
return false;
}
for ($k = 0; $k < $aLink->length; $k++) {
$oLink = $aLink->item($k);
if (strpos($oLink->getAttribute('onclick'), 'return hs.expand(this)') !== false) {
// <a onclick="return hs.expand(this)" href="http://domain.com/uploads/posts/2014-07/1405107411_09072014106.jpg">
// <img title="Some title" alt="Some title" src="http://domain.com/uploads/posts/2014-07/thumbs/1405107411_09072014106.jpg">
// </a>
$oImg = $oLink->firstChild;
$oImg->setAttribute('src', $oLink->getAttribute('href')); // set img proper src
// $oLink->parentNode->removeChild($oLink);
// $oLink->parentNode->replaceChild($oImg, $oLink);
$oLink->parentNode->insertBefore($oImg); // replacing!?!?!?!
// echo $oHtml->ownerDocument->saveHtml($oImg);
}
}
}
Now questions... This code is working BUT I don't get WHY! Why when clearPopUpLink() done with all "images" it has not OLD code with tags? I tried to use (in first time when start investigation) ->insertBefore(), after that ->removeChild(). First is add simple (edited) image BEFOR current image (with <a>), after that delete old node image (with <a>). BUT! It doesn't work, it was doing only on each second (each first was done correctly).
So, let me ask simple question, how to do it in right way? Because I don't think that code below (clearPopUpLink) is correct enough... Please suggest your solutions.
Hmm, I would use the trustee XPath for this and make sure the anchor gets removed; the code you've shown doesn't exactly make that obvious (I haven't tested it).
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a[contains(#onclick, "return hs.expand(this)")]/img') as $img) {
$anchor = $img->parentNode;
$anchor->parentNode->insertBefore($img, $anchor); // take image out
$anchor->parentNode->removeChild($anchor); // remove empty anchor
}
echo $doc->saveHTML();
I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.
This script works well unless you put two URLS in that do not have meta tags, then they render in html all wrong.
How can I use the else statement in it so it will stop?
If you want to test it go here: http://php-playground.co.cc/testdir/metaex.php
<form method="get" action=<?php echo "'".$_SERVER['PHP_SELF']."'";?> >
<p>URL of Competitor:</p>
<textarea name="siteurl" rows="10" cols="50">
<?php //Check if the form has already been submitted and if this is the case, display the submitted content. If not, display 'http://'.
echo (isset($_GET['siteurl']))?htmlspecialchars($_GET['siteurl']):"http://";?>
</textarea><br>
<input type="submit" value="Submit">
</form>
<div id="nofloat"></div>
<table>
<?php
function parseUrl($url){
//Trim whitespace of the url to ensure proper checking.
$url = trim($url);
//Check if a protocol is specified at the beginning of the url. If it's not, prepend 'http://'.
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
//Check if '/' is present at the end of the url. If not, append '/'.
if (substr($url, -1)!=="/"){
$url .= "/";
}
//Return the processed url.
return $url;
}
//If the form was submitted
if(isset($_GET['siteurl'])){
//Put every new line as a new entry in the array
$urls = explode("\n",trim($_GET["siteurl"]));
//Iterate through urls
foreach ($urls as $url) {
//Parse the url to add 'http://' at the beginning or '/' at the end if not already there, to avoid errors with the get_meta_tags function
$url = parseUrl($url);
//Get the meta data for each url
$tags = get_meta_tags($url);
//Check to see if the description tag was present and adjust output accordingly
echo (isset($tags['description']))?"<tr><td>Description($url)</td> <td>".$tags['description']:"Description($url)</td><td>No Meta Description</td></tr>.";
}
}
?>
</table>
Thanks very much!
First, remove the last dot . in the line :
echo (isset($tags['description']))?"<tr><td>Description($url)</td> <td>".$tags['description']:"Description($url)</td><td>No Meta Description</td></tr>.";
EDIT :
I haven't seen this but you have one more error in this line :
Replace ".$tags['description']:" by ".$tags['description'].":
multiple ways to do this; why don't you use simpler way to do this
$tags = NULL;
$tags = get_meta_tags($url);
if($tags)
echo "<tr><td>Description($url)</td><td>" .$tags['description']. "</td></tr>";
else
echo "<tr><td>Description($url)</td<td>No Meta Description</td></tr>";
or if you want to stick with your code try this, need to have staring and ending tags for both true and false;
echo (isset($tags['description'])) ? '<tr><td>Description($url)</td><td>' . $tags['description'] . '</td></tr>' : '<tr><td>Description($url)</td><td>No Meta Description</td></tr>';
Relatively new to php and looking for some help in updating links on a specific page. The page has numerous links eg. href=/link/ and I would like to code the page to identify these links (links that do not already have http or https) and prepend with a url eg. www.domain.com to each. Basically ending up with href=www.domain.com/link/. Any help would be greatly appreciated.
I think you want to parse a list of URLs and prepend "http://" to the ones that don't have it.
<?php
$links = array('http://www.redditmirror.cc/', 'phpexperts.pro', 'https://www.paypal.com/', 'www.example.com');
foreach ($links as &$link)
{
// Prepend "http://" to any link missing the HTTP protocol text.
if (preg_match('|^https*://|', $link) === 0)
{
$link = 'http://' . $link . '/';
}
}
print_r($links);
/* Output:
Array
(
[0] => http://www.redditmirror.cc/
[1] => http://phpexperts.pro/
[2] => https://www.paypal.com/
[3] => http://www.example.com/
)
*/
Maybe it suffices to just change the base URI of the document with the BASE element:
<base href="http://example.com/link/">
With this the new base URI is http://example.com/link/ instead of the URI of the document. That means, every relative URI is resolved from http://example.com/link/ instead of the document’s URI.
You could always use output buffering at the top of your page with a callback that reformats your hrefs to how you'd like them:
function callback($buffer)
{
return (str_replace(' href="/', ' href="http://domain.com/', $buffer));
}
ob_start('callback');
// rest of your page goes here
ob_end_flush();
Because you left out critical details in your first question, here is the second answer.
Doing what #Nev Stokes says may work, but it will also get more than tags. You should never use regular expressions (or, worse, strp_replace) on HTML.
Instead, use the file_get_html() library and do this:
<?php
require 'simplehtmldom/simple_html_dom.php';
ob_start();
?>
<html>
<body>
<a id="id" href="/my_file.txt">My File</a>
<a name="anchor_link" id="boo" href="mydoc2.txt">My Doc 2</a>
PHP Experts
</body>
</html>
<?php
$output = ob_get_clean();
$html = str_get_html($output);
$anchors = $html->find('a');
foreach ($anchors as &$a)
{
if (preg_match('|^https*://|', $a->href) === 0)
{
// Make sure first char is /.
if ($a->href[0] != '/')
{
$a->href = '/' . $a->href;
}
$a->href = 'http://www.domain.com' . $a->href;
}
}
echo $html->save();
Output:
<html>
<body>
<a id="id" href="http://www.domain.com/my_file.txt">My File</a>
<a name="anchor_link" id="boo" href="http://www.domain.com/mydoc2.txt">My Doc 2</a>
PHP Experts
</body>
</html>