How to get url after " : " - php

I have search before & don't find answer
http://i.stack.imgur.com/6mZRz.png
I want to get url of image after " : "
I am using simple dom html
My listing is..
include 'simple_html_dom.php';
$target = 'http://search.aol.com/aol/image?q=aku+ganteng';
$html = file_get_html($target);
foreach($html->find("div[class=inner]") as $f){
$crot = $f->find("img",0)->src;
echo '<img src="'.$crot.'"/><br/>';
}
The HTML listing
<div class="inner">
<span class="imgc"></span>
<a href="imageDetails?s_it=imageDetails&q=aku+ganteng&img=http%3A%2F%2Fsd.keepcalm-o-matic.co.uk%2Fi%2Fjarene-ibuk-ku-aku-ganteng-cok-d.png&v_t=topsearchbox.image&host=http%3A%2F%2Fwww.keepcalm-o-matic.co.uk%2Fp%2Fjarene-ibuk-ku-aku-ganteng-cok-d%2F&width=129&height=151&thumbUrl=https%3A%2F%2Fencrypted-tbn1.gstatic.com%2Fimages%3Fq%3Dtbn%3AANd9GcQD_uhCuZ6yy19yB452fbEQAabTwa3xrOyVdArDf2COKl3AKKYX30dxAht7Nw%3Asd.keepcalm-o-matic.co.uk%2Fi%2Fjarene-ibuk-ku-aku-ganteng-cok-d.png&b=image%3Fs_it%3DimageResultsBack%26v_t%3Dtopsearchbox.image%26q%3Daku%2Bganteng%26oreq%3D310738f642cd4b029e1f8c897168a385&imgHeight=700&imgWidth=600&imgTitle=JARENE+IBUK%26%2339%3BKU+AKU+GANTENG+COK&imgSize=39960&hostName=www.keepcalm-o-matic.co.uk" onclick="return sl.sl(null,null,null,this,'image_results',1)">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQD_uhCuZ6yy19yB452fbEQAabTwa3xrOyVdArDf2COKl3AKKYX30dxAht7Nw:sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png" width="129" height="151" alt="JARENE IBUK'KU AKU GANTENG COK" title="JARENE IBUK'KU AKU GANTENG COK"></a>
</div>
I want get part of this
sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png
How to get full url target?

You probably need this:
$crot= "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQD_uhCuZ6yy19yB452fbEQAabTwa3xrOyVdArDf2COKl3AKKYX30dxAht7Nw:sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png"
preg_match_all('/.*:(.*?)$/sim', $crot, $part, PREG_PATTERN_ORDER);
$part = $part[1][0];
echo $part;
Output:
sd.keepcalm-o-matic.co.uk/i/jarene-ibuk-ku-aku-ganteng-cok-d.png
Full code:
<?
include 'simple_html_dom.php';
$target = 'http://search.aol.com/aol/image?q=aku+ganteng';
$html = file_get_html($target);
foreach($html->find("div[class=inner]") as $f){
$crot = $f->find("img",0)->src;
$ahh = str_replace("thumbs","download",$crot);
$wall = str_replace("t1","1920x1080",$ahh);
preg_match_all('/.*:(.*?)$/sim', $crot, $part, PREG_PATTERN_ORDER);
$part = $part[1][0];
echo $part; //this is what you want.
echo "<a href='$crot'><img src='$crot'/></a><br/>";
}
?>

Related

How to load images and categorise with Unknown Prefix in filename?

This is the code which loads images with different Prefix and the prefix is printed under every image.
I need Prefix as a Title of every set of images as Category of Images
CODE :
$images = glob($dirname . "*.jpg");
foreach ($images as $image) {
if (strpos($image, '#') !== false) {
} else {
?>
<li><?php $mn_img = str_replace("/thumbs", "", $image); ?>
<div class="th [radius]">
<a href="<?php echo $mn_img ?>"><img align="middle"
src="<?php echo $image; ?>"> </a>
</div>
<p style="text-align: center">
<?php
$str = $mn_img;
$s = end(explode("/", $str));
$e = explode(".", $s);
$n = explode('-', $e[0]);
$nm = $n[0];
if ($nm === 'non') {
echo 'general';
} else {
$title = str_replace("_", " ", $nm);
echo $title;
}
?>
</p>
</li>
<?php }
}
I expect the possibility :)
MY Prefix Format is
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Manager_fileoriginalname_random.jpg
Prefix is "Manager"
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Matketing_fileoriginalname_random.jpg
Prefix is "Marketing"
is the preg_match function is what you want :
$returnValue = preg_match( '/(.*)_.*_.*$/', 'Manager_fileoriginalname_random.jpg', $matches );
in $matches[1] you have the prefix, you just need to remplace 'Manager_fileoriginalname_random.jpg' by your var $mn_img
If i can make and advise, can use parse_url() to extract url components : http://php.net/manual/fr/function.parse-url.php

How can I go about getting this link from the youtube API to open in an Iframe in my page or at least open in a modal

Hi I am trying to get a list of videos with the youtube api and then open that into an iframe that is on my site. The script below will retrieve a list of videos by keyword but It will only link to the page that the video is on and I would obviously like to open it into the iframe on my page. Or at least open the youtube embed in a modal. The $watch variable get the url but it comes back as https://www.youtube.com/watch?v=videoID&feature=youtube_gdata_player. This will not open in an iframe even if I put it there manually.
Is there a way to parse this. Or return it with the full embed instead of watch=tv. I also am not sure if there is a way to echo target="video-frame" in the <a> tag that is created. Is there a different variable that I need to use to get the video url and is how would I write this to echo it as for some reason when I do the server denies the page.
<?php
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/-/Keyword/';
$sxml = simplexml_load_file($feedURL);
$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
$total = $counts->totalResults;
?>
<?php
foreach ($sxml->entry as $entry) {
$media = $entry->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$rating = $attrs['average'];
} else {
$rating = 0;
}
echo "<li>\n";
echo "{$media->group->title}
<br/>\n";
echo sprintf("%0.2f", $length/60) . " min. | {$rating} user rating
<br/>\n";
echo "{$media->group->description}<p/>\n";
echo "<p/></li>\n";
}
?>
You can get VideoID and then put it in iframe.
Here is the code:
<?php
$string = "https://www.youtube.com/watch?v=videoID&feature=youtube_gdata_player";
$startpoint=strpos($string,'watch?v=');$startpoint=$startpoint+8;
$length=strpos($string,'&');$length = $length - $startpoint;
$videoId = substr($string,$startpoint,$length);
$ytstring = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'.$videoId.'" frameborder="0" allowfullscreen></iframe>';
echo $videoId;
echo $ytstring;
?>
First of all, I would recommend to go with JSON instead of XML feed. In order to accomplish loading videos into Iframe, you'll need to use Javascript.
Here's a quick demo you can use for reference:
<?php
//YouTube json feed example:
$keyword = "basketball";
$file = file_get_contents("https://gdata.youtube.com/feeds/api/videos/-/$keyword?v=2&alt=json");
//decode into array
$decoded = json_decode($file, true);
//point the feed to 'entry' array
$entries = $decoded['feed']['entry'];
//parse through entries
if (!empty($entries)) {
for($i=0; $i<count($entries); $i++) {
$thumb = $entries[$i]['media$group']['media$thumbnail'][0]['url'];
$title = $entries[$i]['title']['$t'];
$description = $entries[$i]['media$group']['media$description']['$t'];
$video = "https://www.youtube.com/embed/".$entries[$i]['media$group']['yt$videoid']['$t']."?wmode=opaque";
$published = date('Y-m-d H:i:s', strtotime($entries[$i]['published']['$t']));
$content[]= "<img src=\"$thumb\"><br />$title<br /><small>$description<hr />$published</small>";
}
}
?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
function yt_load_video(url) {
$("iframe").attr("src",url);
return;
}
</script>
<?php if (!empty($content)) : ?>
<iframe type="text/html" src="https://www.youtube.com/embed/<?php echo $entries[0]['media$group']['yt$videoid']['$t']; ?>?wmode=opaque" frameborder="0" width="400" height="300"></iframe>
<hr />
<?php foreach($content as $thumb) : ?>
<div style="float:left; margin:4px; width:200px; height:200px; overflow:scroll"><?php echo $thumb ?></div>
<?php endforeach; ?>
<?php endif; ?>

Extract Title and Description from multiple URLs

I have a list of links that are all articles. I am trying to use PHP to extract the title and the description from all of them at once. I also want the article title to be hyperlink to the URL and the description to be displayed below it in italics.
My issue is this: it works when I do it for one link, but when I try multiple links or even if I duplicate the code and manually paste in each link, it doesn't work. Below is my code that I have that works for one link. Any ideas?
<html>
<a href="http://bit.ly/18EFx87">
<b><?php
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}
echo getTitle("http://bit.ly/18EFx87");
?></b><br>
</a>
<i><?php
$tags = get_meta_tags('http://bit.ly/18EFx87');
echo $tags['description'];
?></i>
</html>
I assume you mean multiple URLs, then something like this will work. :
<html>
<?php
function getTitle($url){
#$str = file_get_contents($url); // suppressing the warning
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
} else {
return false;
}
}
$urls = array('http://bit.ly/18EFx87', 'url2');
foreach($urls as $url)
{
$title = getTitle($url);
if($title === false)
{
continue;
}
echo '<a href="' . $url . '"><b>';
echo $title;
echo '</b></a><br><i>';
$tags = get_meta_tags($url);
echo $tags['description'] . '</i>';
}
?>
</html>

parsing url in bbcode

Hi I'm developing a custom forum on my website. I would like to convert the urls starting with : http://*.domain.com/photos/{username}/{photo_id} (I should get both username and photo_id ) to the direct image tag so that user get the image instead of the url.
This should be done if they insert the url with or without bbcode:
ie:
http://domain.com/photos/musthafa/12345
[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]
[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]
This should be converted to < html-imge tag src="url-to_photo-path/photo_id.j_p_g" />
I tried this:
$str = "http://www.domain.com/photos/musthafa/12345"
$str = preg_replace_callback("'\[url=http:\/\/www\.domain\.com\/photos\/(.*?)\](.*?)\[/url\]'i", 'self::parse_photo_url', $str);
AND
$str = preg_replace_callback("#^https?://([a-z0-9-]+\.)domain.com/photos/(.*?)$#", 'self::parse_gpp_photo', $str);
function parse_photo_url($url){
{
$full_url = "http://www.domain.com/" . $url[1];
$url_segs = parse_url($full_url);
$path = explode("/", $url_segs['path']);
return '<img src="http://www.domain.com/{path-to-the-gallery}/'.$path[2].'/jpg" />';
}
Musthafa.
I'm not sure that I've exactly got what you want but please try this:
<?php
$image_url_samples = array(
"http://domain.com/photos/musthafa/12345",
"[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]",
"[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]"
);
foreach ($image_url_samples as $image_url_sample)
{
$conversion_result = preg_replace("/^(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)$/i",
"<img src=\"\\1.jpg\" alt=\"\\1\" />", $image_url_sample);
$conversion_result = preg_replace("/^\\[url=(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)\\](.+)\\[\\/url\\]$/",
"<img src=\"\\1.jpg\" alt=\"\\2\" />", $conversion_result);
print $conversion_result . "<br />";
}
The output is:
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="my photo link here" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
By the way, if you want to make your URL case insensitive add the i modifier to the end of regular pattern (PHP Pattern Modifiers).
Basically I would like to extract the id (12345 in this example) and I need to extract the direct link to the image url so that, user should get the image tag instead of url.
Thats why I called
preg_replace_callback function.
In simple words, I'm stuck at the regex pattern to match my domain url starting with:
http://domain.com/photos{username}/{id}
OR
http://www.domain.com/photos{username}/{id}"
You don't need regex.
function buildLink( $id, $name ){
$html = array();
$html[] = '<img alt="" src="http://www.domain.com/photos/';
$html[] = $name;
$html[] = '/';
$html[] = $id;
$html[] = '.jpg">';
return implode( '', $html );
}
$path = 'http://www.domain.com/photos/musthafa/12345';
$path = rtrim( $path, ' /' );
$path_parts = explode( '/', $path );
$id = ( int ) array_pop( $path_parts );
$name = array_pop( $path_parts );
$img = buildLink( $id, $name );
echo $img;

Simple HTML Dom

Thanks for taking the time to read my post... I'm trying to extract some information from my website using Simple HTML Dom...
I have it reading from the HTML source ok, now I'm just trying to extract the information that I need. I have a feeling I'm going about this in the wrong way... Here's my script...
<?php
include_once('simple_html_dom.php');
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");
// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);
$html = file_get_html('http://myshop.com/small_houses.html');
$html .= file_get_html('http://myshop.com/medium_houses.html');
$html .= file_get_html('http://myshop.com/large_houses.html');
//Define my variable for later
$product['image'] = '';
$product['title'] = '';
$product['description'] = '';
foreach($html->find('img') as $src){
if (strpos($src->src,"http://myshop.com") === false) {
$src->src = "http://myshop.com/$src->src";
}
$product['image'] = $src->src;
}
foreach($html->find('p[class*=imAlign_left]') as $description){
$product['description'] = $description->innertext;
}
foreach($html->find('span[class*=fc3]') as $title){
$product['title'] = $title->innertext;
}
echo $product['img'];
echo $product['description'];
echo $product['title'];
?>
I put echo's on the end for sake of testing...but I'm not getting anything... Any pointers would be a great HELP!
Thanks
Charles
file_get_html() returns a HTMLDom Object, and you cannot concatenate Objects, although HTMLDom have __toString methods when there concatenated there more then lilly corrupt in some way, try the following:
<?php
include_once('simple_html_dom.php');
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");
// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);
$pages = array(
'http://myshop.com/small_houses.html',
'http://myshop.com/medium_houses.html',
'http://myshop.com/large_houses.html'
)
foreach($pages as $page)
{
$product = array();
$source = file_get_html($page);
foreach($source->find('img') as $src)
{
if (strpos($src->src,"http://myshop.com") === false)
{
$product['image'] = "http://myshop.com/$src->src";
}
}
foreach($source->find('p[class*=imAlign_left]') as $description)
{
$product['description'] = $description->innertext;
}
foreach($source->find('span[class*=fc3]') as $title)
{
$product['title'] = $title->innertext;
}
//debug perposes!
echo "Current Page: " . $page . "\n";
print_r($product);
echo "\n\n\n"; //Clear seperator
}
?>

Categories