using simple html dom to find facebook username - php - php

I'm using php and the simple html dom
http://simplehtmldom.sourceforge.net/manual.htm
I'm trying to extract the facebook, twitter, google, youtube and pinterest usernames from the addthis code below..
<!-- AddThis Follow BEGIN -->
<div class="addthis_toolbox addthis_default_style" style="margin-top:-3px;">
<a tabindex="2" class="addthis_button_facebook_follow" style="padding:3px 6px;" addthis:userid="namehere"></a>
<a tabindex="3" class="addthis_button_twitter_follow" style="padding:3px 6px;" addthis:userid="namehere"></a>
<a tabindex="4" class="addthis_button_google_follow" style="padding:3px 6px;" addthis:userid="namehere"></a>
<a tabindex="5" class="addthis_button_youtube_follow" style="padding:3px 6px;" addthis:userid="namehere"></a>
<a tabindex="6" class="addthis_button_pinterest_follow" style="padding:3px 6px;" addthis:userid="namehere"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-51e6691575bd9b10"></script>
<!-- AddThis Follow END -->
How can I get the usernames using the simple html dom?
I tried running this...
$html = file_get_html('http://www.domain.com/');
foreach($html->find('a[class=addthis_button_facebook_follow]') as $element)
echo $element->class;
which outputs the class name:
addthis_button_facebook_follow
but how do I return the addthis:userid value?
addthis:userid

Actually, you're not calling the attribute right. Consider this example:
// target the first link, since you only need one, and target the proper attribute
$id = $html->find('a[class="addthis_button_facebook_follow"]', 0)->{'addthis:userid'}; // first link facebook
echo $id; // namehere

Try:
echo $element->{'addthis:userid'};

Related

(PHP) Trying to controll the include function

I am now done making my website dynamic (control multiple pages througe 1 page)
I have template where I have 2 side bars left and right, right for categories, left for the the many pages of each category.
Now using the include function to dynamicly generate the side bars is great,
but i am also using class:current on these side bars so the user can easily detect where he is.
now with the right sidebar there is no problem because if the user is in HTML category it will be highlighted with class:current but will not need to change, because user is still in HTML category.
but if the user switch page the left side bar will stay with the last class:current (for example html intro ), even tho user switched to next page (html basics).
i can only have 1 class:current when using the include function.
how can i controll that?
the code below is the file that im including to each page dynamicly it is .php file.
<div class="sidenav">
<h3> HTML Collection</h3>
<hr class="hr1">
<a class="current" style="cursor: pointer;" onclick="window.location.href = 'html_default.php';"> HTML Intro</a>
<a style="cursor: pointer;" onclick="window.location.href = 'html_editors.php';"> HTML Editors</a>
<a style="cursor: pointer;" onclick="window.location.href = 'html_basics.php';"> HTML Basics</a>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
<div class="sidenavright">
<h3> Categories</h3>
<hr class="hr1">
<a style="cursor: pointer;" onclick="window.location.href = '../javascript/js_intro.php';"> Javascript</a>
<hr class="hr1" />
<a class="current" style="cursor: pointer;" onclick="window.location.href = '../html/html_default.php';"> HTML</a>
<hr class="hr1" />
<a style="cursor: pointer;" onclick="window.location.href = '../howtomain/howto_default.php';"> HowTo</a>
<hr class="hr1" />
<a style="cursor: pointer;" onclick="window.location.href = '../python/python_intro.php';"> Python</a>
<hr class="hr1" />
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
You should be doing this all though JavaScript really, something like AngularJS, or maybe even use Laravel and it would be very easy to control it in your blade views.
Here’s something you can do however.
From the file where you are including it, define a variable before the include that will allow the include to know what page you’re on.
Like so:
$currPage = “page1”;
Then in your include, on each tag, you can programmatically put your current class on using one line if/elses, like so:
<a <?php ($currPage == “page1”) ? echo ‘class=“current”’ : echo ‘’?>>Page 1</a>
And repeat for all pages you want.
One line if/elses in PHP go like this:
Condition ? //true : //false
Good luck

Extract links from specific table

I have a html code with many html tables. I want to extract links from specific one which has specific div above.
Here's my sample code:
<div class="boxuniwersal_header">Table 1</div>
<img src="img/boxuniwersal_top.gif" width="210" height="18" alt="" style="margin-top: 5px" />
<div class="boxuniwersal_content">
<div class="boxuniwersal_subcontent">
<div class='menu_m1'><table cellpadding="3"><tr><td><img src="some.jpg" width="45" /></td><td>Some text</td></tr></table></div>
<br />
</div>
</div>
<!-- /box -->
<!-- box -->
<div class="boxuniwersal_header">Table 2</div>
<img src="img/boxuniwersal_top.gif" width="210" height="18" alt="" style="margin-top: 5px" />
<div class="boxuniwersal_content">
<div class="boxuniwersal_subcontent">
<div class='menu_m1'><table cellpadding="3"><tr><td><img src="some2.jpg" width="45" /></td><td>Some text2</td></tr></table></div>
<br />
</div>
</div>
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query("//div/div/table/tr/td/a|//table//tr/td//a"); //querying domdocument
foreach($results as $result)
{
$links[]=$result->getAttribute("href");
}
This code returns all links. I want to grab only links from Table1. Is it possible?
Your main problem is just tuning the XPath expression to select the right XML.
If you change your XPath to
//div[text()="Table 1"]/following-sibling::div[1]//table//a
What this does is first find the <div> element whose text is the one your after.
The following-sibling::div[1] part will look at the first <div> element at the same level as the <div> element already selected (this is the one where the <table> is).
The last part just looks for all <a> elements within the enclosing <table>.

Wordpress jQuery not working due to active shortcode in PHP file

Here is the code-
<div class="container contact-form">
<p id="contact" class="pg-title contact"> Contact <br>
<span> <img class="narro-line" src="<?php echo get_template_directory_uri(); ?>/img/narro-line.png" alt="" /></span> </p>
<?php echo do_shortcode('[gravityform id="1" title="false" description="false"]'); ?>
</div>
jQuery plugins not working with including the shortcode <?php echo do_shortcode('[gravityform id="1" title="false" description="false"]'); ?> . But when I remove the shortcode from here then the jQuery is working properly. Where is the error?
try to use jQuery instead of $.
or if you want to use $ just define it in the on load function like below.
jQuery(document).ready(function($){
$(selector).event();
//from now you can use $
});

Find a Div having spaces in its classname HTML DOM

How do I find a div in PHP using HTML DOM which has spaces in its classname.
Snippet
<div class="f14 rel">
<input type="checkbox" class="p0m0 mr_5" name="propid[]" id="X6849129" value="X6849129"/>
<div id="imgChkBoxX6849129">
<a id="desc_X6849129" class="f14 uline" target="_blank" href="some link"/>
</div>
I tried $html->find('div."f14 rel"').But it doesn't work.Neither does $html->find('div.f14 rel')

What would be the best way to make a widget that will link to 4 different websites?

I'm trying to limit maintnance headaches by avoiding having to copy and paste code and having to update it on several different sites. Should I use an iframe? So far I just used inline CSS to style it and plan on copying a pasting to 3 or 4 other sites. Kind of like:
<div style="width:165px; height:40px; background: url('http://site1.com/images/DH-sharebar.gif') repeat-x top #333;float:right;margin-top:15px;margin-right:20px;border-radius:5px;border:1px #565656 solid;">
<a href="http://site1.com/" target="_blank" title="site 1">
<img src="http://davidhairabedian.com/davidhairabedian/images/DH-sharebar-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
<a href="http://site2.org/" target="_blank" title="site 2">
<img src="http://site1.com/images/DH-sharebar-HPM-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
<a href="http://site3.org/" target="_blank" title="site 3">
<img src="http://site1.com/images/DH-sharbar-EHF.png" style="border-radius:0;margin-top:5px;margin-left:3px;">
</a>
<a href="http://site4.org/" target="_blank" title="site 4">
<img src="http://site1.com/images/DH-sharebar-EHC.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
</div>
If you don't mind having a slight delay, you could write a light JavaScript that loaded the content into the page via AJAX, much like facebook / many other widgets do.
The benefit that offsets the fact that your links are not part of the page's initial HTML is the fact that you can update the content of all widgets from one place, with no chance that you'll forget one int he future.
Have a look into how Facebook / Google + / Twitter / Everyone else does this.
Edit
Your question got me thinking about how one might do this, so I did it. I've made a working JSFiddle example.
Basically, you paste an empty div and a script tag into your target pages. The script references a file stored on your central server. It creates another script tag in the document, which itself contains a call to a function defined in the first script, which inserts your widget into the specified div on the page.
Pasted into your pages
<div id="placeholder-div"></div>
<script type="text/javascript" src="http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/script.js"></script>
First script content
(function loadContent() {
(function xss_ajax(url) {
var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/content.php');
script.setAttribute('id', 'script_id');
script_id = document.getElementById('script_id');
if(script_id){
document.getElementsByTagName('head')[0].removeChild(script_id);
}
// Insert <script> into DOM
document.getElementsByTagName('head')[0].appendChild(script);
})();
})();
function callback(data) {
document.getElementById('placeholder-div').innerHTML = data;
}
Inserted script content:
<?php ob_start(); ?>
<div style="width:165px; height:40px; background: url('http://site1.com/images/DH-sharebar.gif') repeat-x top #333;float:right;margin-top:15px;margin-right:20px;border-radius:5px;border:1px #565656 solid;">
<a href="http://site1.com/" target="_blank" title="site 1">
<img src="http://davidhairabedian.com/davidhairabedian/images/DH-sharebar-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
<a href="http://site2.org/" target="_blank" title="site 2">
<img src="http://site1.com/images/DH-sharebar-HPM-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
<a href="http://site3.org/" target="_blank" title="site 3">
<img src="http://site1.com/images/DH-sharbar-EHF.png" style="border-radius:0;margin-top:5px;margin-left:3px;"/>
</a>
<a href="http://site4.org/" target="_blank" title="site 4">
<img src="http://site1.com/images/DH-sharebar-EHC.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
</div>
<?php $content = json_encode(ob_get_clean());
echo "callback($content);";
And after all this, it occurred to me that you could just use an iframe:
<iframe src="http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/iframe.html"></iframe>
Personally, I would use the JavaScript method, as this would allow me to modify the style of the widget whenever I wanted, without requiring my users to update their pages.

Categories