So far what i have been trying to do is output a video response i know it wont work if i dont have the $id after the tag but before the tag it wont even work if the str_replace doesn't work.
<?php
$html = "";
$url = "https://www.youtube.com/feeds/videos.xml?user=Fliberjig1";
$xml = simplexml_load_file($url);
for($i = 0; $i < 6; $i++) {
$id = $xml->entry[$i]->id;
$id = str_replace("YT:VIDEO:","", $id);
$title = $xml->entry[$i]->title;
$html .= "<li><h3>$title</h3>
<p>$id</p>
<iframe width='854' height='480' src='https://www.youtube.com/embed/$id' frameborder='0' allowfullscreen></iframe></li>";
}
echo $html;
?>
the result will be like this
YT:VIDEO:WOCIEMNSI4C
but i want it to be like this
WOCIEMNSI4C
Can you please help in any way to help me with my problem
If I understand you correctly, you just want to parse the string. This can be done using:
$pieces = explode(":", $id);
$clean_id = $pieces[2];
It seems that the feed contains yt:video:WOCIemNSI4c and not YT:VIDEO:WOCIEMNSI4C so what you actually need is the str_ireplace
function instead of str_replace you're currently using.
So if you change your 7th line of code to this:
$id = str_ireplace("YT:VIDEO:","", $id);
you should be ok.
$id = "YT:VIDEO:WOCIEMNSI4C";
$rest = substr("$id", -11); //result WOCIEMNSI4C
Related
I have never used XML before and am trying to loop through the XML and display all the display names in the 'A Team'. The code I am using is outputting 10 zeros and not the names.
The code I am using is attached below along with the feed.
Any assistance is much appreciated!
feed: https://apn.apcentiaservices.co.uk/ContactXML/agentfeed?organisation=se724de89ca150f
<?php
$url = 'https://apn.apcentiaservices.co.uk/ContactXML/agentfeed?organisation=se724de89ca150f';
$html = "";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->organisation['APN']->brand['AllStar Psychics']->pool['A Team']->agent[$i]->display-name;
echo $title;
}
echo $html;
?>
This might getthe basics you asked for. Not sure if it's what you want. I'm not that good at xpath.
$mydata = $xml->xpath('/organisation/brand/pool[#name="A Team"]//display-name');
foreach($mydata as $key=>$value){
echo('Name:' . $value .'<br>');
}
Hello I am having trouble with array to string conversion in the following script.
The problem is on the top if statement. For some reason it works fine in the else statement underneath but I keep getting a array to string conversion error when submitting my form and I'm not sure why it works before submitting the form but not after.
The problem is apparently in the first
$content[] = array.
It just returns the word array for $video variable. So where it states the videoid in data-videoID="" it just comes up as data-videoId="Array". Everything esle comes up fine. This would be on line 12 and 13. Here is the code I am using. Im sure it probably something simple that I am overlooking because I am in no way as experienced with php as I probably should be. I have looked over other posts and have tried taking off the brackets and several other things but still cant figure it out. Like I said it works fine if I take out the if statement and just use the else statement. Here is the code.
if(isset($_GET['q'])) {
$keyword_q = $_GET['q'];
$keyword = preg_replace('/\s+/', '/', $keyword_q);
$file = file_get_contents("https://www.googleapis.com/youtube/v3/search?videoEmbeddable=true&videoType=any&part=id%2Csnippet&q=$keyword&videoSyndicated=true&type=video&maxResults=50&key=$key");
$decoded = json_decode($file, true);
$entries = $decoded['items'];
if (!empty($entries)) {
for($i=0; $i<count($entries); $i++) {
$thumb = $entries[$i]['snippet']['thumbnails']['medium']['url'];
$videotitle = $entries[$i]['snippet']['title'];
$videodescription = $entries[$i]['snippet']['description'];
$video = $entries[$i]['id'];
$content[] = "<li><a href='#' data-videoID='$video' class='video-link'><img src='$thumb' alt='Play Video'></a><h3 class='video-title'>$videotitle</h3><p class='video-description'>$videodescription</p></li>";
}
}
$videos = "";
if (!empty($content)) {
foreach($content as $thumb){
$videos .= "$thumb";
}
}
}
else {
$filefeatured = file_get_contents("https://www.googleapis.com/youtube/v3/videos?chart=mostPopular&part=snippet&maxResults=30&key=$key");
$decoded = json_decode($filefeatured, true);
$entries = $decoded['items'];
if (!empty($entries)) {
for($i=0; $i<count($entries); $i++) {
$thumb = $entries[$i]['snippet']['thumbnails']['medium']['url'];
$videotitle = $entries[$i]['snippet']['title'];
$videodescription = $entries[$i]['snippet']['description'];
$video = $entries[$i]['id'];
$content[]= "<li><a href='#' data-videoID='$video' class='video-link'><img src='$thumb' alt='Play Video'></a><h3 class='video-title'>$videotitle</h3><p class='video-description'>$videodescription</p></li>";
}
}
$videos = "";
if (!empty($content)) {
foreach($content as $thumb){
$videos .= "$thumb";
}
}
}
I have a string like:
$description = '
1st link in string,
2nd link in string, some other text in string,
3rd link in string.
';
I need to use preg_replace to append a query parameter of "?id=awesome" to any urls in the $description string from "replace.com" (ignoring all other links, i.e. "ignore.com").
Thanks in advance for any help!
I would strongly advise using a DOM parser instead of a regex. For instance:
$description = '...';
$wrapper = "<root>".$description."</root>";
$dom = new DOMDocument();
$dom->loadXML($wrapper);
$links = $dom->getElementsByTagName('a');
$count = $links->length;
for( $i=0; $i<$count; $i++) {
$link = $links->item($i);
$href = $link->getAttribute("href");
if( preg_match("(^".preg_quote("http://www.replace.com/").")i",$href))
$link->setAttribute("href",$href."?id=awesome");
}
$out = $dom->saveXML();
$result = substr($out,strlen("<root>"),-strlen("</root>"));
Ok, simple enough, here you go:
$content = preg_replace('/http:\/\/[^"]*?\.replace\.com\/[^"]+/','$0?id=awesome',$description);
Hope that's it, $content will have the string witht he added paramters to the replace.com domain :)
I'm still working on this catalogue for a client, which loads images from a remote site via PHP and the Simple DOM Parser.
// Code excerpt from http://internetvolk.de/fileadmin/template/res/scrape.php, this is just one case of a select
$subcat = $_GET['subcat'];
$url = "http://pinesite.com/meubelen/index.php?".$subcat."&lang=de";
$html = file_get_html(html_entity_decode($url));
$iframe = $html->find('iframe',0);
$url2 = $iframe->src;
$html->clear();
unset($html);
$fullurl = "http://pinesite.com/meubelen/".$url2;
$html2 = file_get_html(html_entity_decode($fullurl));
$pagecount = 1;
$titles = $html2->find('.tekst');
$images = $html2->find('.plaatje');
$output='';
$i=0;
foreach ($images as $image) {
$item['title'] = $titles[$i]->find('p',0)->plaintext;
$imagePath = $image->find('img',0)->src;
$item['thumb'] = resize("http://pinesite.com".str_replace('thumb_','',$imagePath),array("w"=>225, "h"=>162));
$item['image'] = 'http://pinesite.com'.str_replace('thumb_','',$imagePath);
$fullurl2 = "http://pinesite.com/meubelen/prog/showpic.php?src=".str_replace('thumb_','',$imagePath)."&taal=de";
$html3 = file_get_html($fullurl2);
$item['size'] = str_replace(' ','',$html3->find('td',1)->plaintext);
unset($html3);
$output[] = $item;
$i++;
}
if (count($html2->find('center')) > 1) {
// ok, multi-page here, let's find out how many there are
$pagecount = count($html2->find('center',0)->find('a'))-1;
for ($i=1;$i<$pagecount; $i++) {
$startID = $i*20;
$newurl = html_entity_decode($fullurl."&beginrec=".$startID);
$html3 = file_get_html($newurl);
$titles = $html3->find('.tekst');
$images = $html3->find('.plaatje');
$a=0;
foreach ($images as $image) {
$item['title'] = $titles[$a]->find('p',0)->plaintext;
$item['image'] = 'http://pinesite.com'.str_replace('thumb_','',$image->find('img',0)->src);
$item['thumb'] = resize($item['image'],array("w"=>225, "h"=>150));
$output[] = $item;
$a++;
}
$html3->clear();
unset ($html3);
}
}
echo json_encode($output);
So what it should do (and does with some categories): Output the images, the titles and the the thumbnails from this page: http://pinesite.com
This works, for example, if you pass it a "?function=images&subcat=antiek", but not if you pass it a "?function=images&subcat=stoelen". I don't even think it's a problem with the remote page, so there has to be an error in my code.
Ehm..trying to state the obvious maybe but 'stoele'?
As it turns out, my code was completely fine, it was a missing space in the HTML of the remote site that got the Simple PHP DOM Parser to not recognize the iframe I was looking for. I fixed it on my end by running a str_replace on the code first to replace the faulty code.
I know it's a dirty solution, but it works :)
I need to find links in a part of some html code and replace all the links with two different absolute or base domains followed by the link on the page...
I have found a lot of ideas and tried a lot different solutions.. Luck aint on my side on this one.. Please help me out!!
Thank you!!
This is my code:
<?php
$url = "http://www.oxfordreference.com/views/SEARCH_RESULTS.html?&q=android";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<table class="short_results_summary_table">');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);
echo "{$table}";
$dom = new DOMDocument();
$dom->loadHTML($table);
$dom->strictErrorChecking = FALSE;
// Get all the links
$links = $dom->getElementsByTagName("a");
foreach($links as $link) {
$href = $link->getAttribute("href");
echo "{$href}";
if (strpos("http://oxfordreference.com", $href) == -1) {
if (strpos("/views/", $href) == -1) {
$ref = "http://oxfordreference.com/views/"+$href;
}
else
$ref = "http://oxfordreference.com"+$href;
$link->setAttribute("href", $ref);
echo "{$link->getAttribute("href")}";
}
}
$table12 = $dom->saveHTML;
preg_match_all("|<tr(.*)</tr>|U",$table12,$rows);
echo "{$rows[0]}";
foreach ($rows[0] as $row){
if ((strpos($row,'<th')===false)){
preg_match_all("|<td(.*)</td>|U",$row,$cells);
echo "{$cells}";
}
}
?>
When i run this code i get htmlParseEntityRef: expecting ';' warning for the line where i load the html
var links = document.getElementsByTagName("a"); will get you all the links.
And this will loop through them:
for(var i = 0; i < links.length; i++)
{
links[i].href = "newURLHERE";
}
You should use jQuery - it is excellent for link replacement. Rather than explaining it here. Please look at this answer.
How to change the href for a hyperlink using jQuery
I recommend scrappedcola's answer, but if you dont want to do it on client side you can use regex to replace:
ob_start();
//your HTML
//end of the page
$body=ob_get_clean();
preg_replace("/<a[^>]*href=(\"[^\"]*\")/", "NewURL", $body);
echo $body;
You can use referencing (\$1) or callback version to modify output as you like.