Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
<a data-track='' _sp= class=s-item__link href=get_this_href>...</a>
With the above link, the data-track contains some json data. The _sp= could contain numbers/letters and a period (.). The class is s-item__link.
I would need the get_this_href and then I can go from there.
This is the regex I tried... but im stuck from here.
<a\b(?=[^>]* class="[^"]*(?<=[" ])s-item__link[" ])(?=[^>]* href="([^"]*))
Here is an example: https://regex101.com/r/rVPeUI/1
$link = ""; //url im scraping
$html = file_get_html($link);
//find is part of simple_html_dom.php. im saying each li item is an $item.
foreach ($html->find('li.s-item ') as $item) {
//$item contains the decent amount of nested divs with spans and links.
}
Without using Regex, its better to use DOMDocument() to parse HTML tags:
$doc = DOMDocument::loadHTML($html);
$xpath = new DOMXPath($doc);
$query = "//a[#class='s-item__link']";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo "HREF " . $entry->getAttribute("href");
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a xml with more subcategory.
I want extract in Php the “long_name” where type is “adminkstrative_area_level_3”
How I can do?
This is my xml https://ibb.co/fY27bJ
I tried but don’t work
<?
$string_data = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI";
$xml = simplexml_load_string($string_data);
$citta = (string) $xml->result[0]->address_component[3]->long_name;
echo "<p>".$citta."</p>";
?>
You are missing geoname in $xml-> name;
Try it like this:
$xml = simplexml_load_string($string_data);
$citta = (string)$xml->geoname->name;
echo $citta;
Demo Php
If you want to loop through mulitple items you could use:
foreach ($xml->geoname as $item) {
echo $item->name;
}
Update:
For the updated part you could use the same technique:
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI");
foreach ($xml->result as $item) {
if ((string)$item->type === "administrative_area_level_3") {
echo $item->address_component->long_name;
}
}
Or by index [1]:
echo $xml->result[1]->address_component->long_name;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I execute on my main page some php code which includes :
foreach ($fbdata->feed->data as $fbpost)
{
...
}
How can we convert this , into a loop that goes from (i to z)(0 to 10) ?
Simple for loop
for($i = 0; $i < 10; $i++) {
$fbpost = $fbdata->feed->data[$i];
...
}
or if you like to use the as, try using a foreach but slicing the array before using it
$fbPosts = array_slice($fbdata->feed->data, 0, 10);
foreach($fbPosts as $fbpost) {
...
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to get specific content from a remote web page
The content I want to get are inside javascript variables, this kind :
var Example1 = 0;
var Example2 = 14;
The name of the variable remain the same and the content is only numbers
Thank you
Find scripts in html source by DomDocument and then variable declaration by regex
$DOM = new DomDocument();
$DOM->loadHTML( $output);
$res = [];
$scripts = $DOM->getElementsByTagName('script');
$lnt = $scripts->length;
for($i=0; $i < $lnt; $i++) {
preg_match_all('/var\s+(\w+)\s*=\s*(\d+)\s*;/', $DOM->saveHtml($scripts->item($i)), $m);
$res = array_merge($res, array_combine($m[1], $m[2]));
}
print_r($res);
demo
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
http://plnkr.co/edit/06lu6r34eGNfRq1fygNy
I want to only display 10 "cards" from my array, how would I do so?
I tried using unset() but it is very inefficient and doesn't even work for multiple cards.
Also how can I make the code display the "cards" horizontally instead of vertically?
If you’d like to use unset, the correct syntax would be unset($cards[1]); unset($cards[2]); unset($cards[3]); ... and so on.
However, for your particular situation, I’ll recommend you to use array_slice:
$cards = array(
"Messi", "Ronaldo", "Ibrahimovic", "Ribery", "Robben", "Neymar", "Rooney", "Casillas",
"Falcao", "Van Persie", "Hazard", "Iniesta", "Xavi", "Schweinsteiger", "Silva", "Fabregas",
"Lahm", "Aguero", "Cavani", "Vidic", "Ozil", "Mata", "Bale", "ThiagoSilva",
"Kompany", "Tevez", "Toure", "Ramos", "Suarez", "Pirlo", "DiMaria", "Neuer",
"Pique", "Buffon", "Lewandowski", "Gomez", "Chiellini", "Cole", "Pedro", "Busquets",
"Cech", "Muller", "Hummels", "Alonso", "Navas", "Modric", "Cazorla", "Gotze",
"Benzema", "Vidal", "Lavezzi"
);
shuffle($cards);
$cards = array_slice($cards, 0, 10);
For the horizontal display, you may simply omit the <br> at the end of every iteration of your loop, but, depending on the resolution of the user’s browser, the images may occupy more than one line. For a strictly horizontal arrangement, use an HTML table with 10 columns:
print("<table><tr>");
foreach($cards as $card){
$img = "http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/".$card.".png";
print("<td><img src=\"".$img."\"/></td>");
}
print("</tr></table>");
The following works too:
for ($i = 0; $i < 10; $i++) {
echo $cards[$i] ." <br>"; // put the name above the card
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'> <br>";
}
If you want them horizontally, and without names, don't put in the <br>:
for ($i = 0; $i < 10; $i++) {
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'>";
}