First of all, i'm new to PHP. I've got the following code-snippet and it works pretty well.
<?php
$html = file_get_contents('http://www.example.com/');
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i',$html, $matches );
foreach ($matches[1] as $match) {
echo '<img src="' . $match . '" />';
}
?>
Now i want to add the file_get_contents url after a questionmark in the filename like:
http://www.example.com/getfile.php?http://www.url.com/images/
How to do this? Is this cURL?
Solution:
Changing:
$html = file_get_contents('http://www.example.com/');
to
$html = file_get_contents($_GET["url"]);
works! :)
Related
i need some help for get images. Im using preg_match_all function.
Source: <img alt="test" title="test" src="/data/brands/test.png">
how can i get full image url ?
And this is my code for text. I need add image here.
<?
$link = 'link';
$marka = '#<div class="test">(.*?)</div>#si';
$getir = file_get_contents($link);
preg_match_all($marka,$getir,$test1);
$test = $test1[0];
echo $test[0]; ?>
Thanks.
This might help you
$str = '<img alt="test" title="test" src="/data/brands/test.png">';
$regex = '#src="(.+?)">#';
preg_match($regex,$str,$match);
echo $match[1];
//prints: /data/brands/test.png
I want to insert some input a list into JSON but it looks very strange. I use strip_tags to fix a memory leak. Here is the code and the outputted JSON
Code:
<?php
echo '<meta charset="utf-8">';
include_once('libs/simplehtmldom/simple_html_dom.php');
$html = file_get_html('https://hugo.events/event/2017');
// Find all links
foreach($html->find('ul#EventLandinLineUpList') as $element)
$encoded = strip_tags($element);
echo json_encode($encoded);
And this is the JSON that has been outputted:
"\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFEDDE LE GRAND\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tKENSINGTON\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tTYPHOON\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tSUNDAY SUN\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tBL\u00d8F\n\t\t\t\t\t\t\t\t\t\t\t\t\t"
I hope someone can help me. Thanks in advance
You can use trim() to remove the whitespace there.
Eventually I used: preg_replace.
This was the final code:
<?php
echo '<meta charset="utf-8">';
include_once('libs/simplehtmldom/simple_html_dom.php');
$html = file_get_html('https://hugo.events/event/2152');
// Find all links
foreach($html->find('ul#EventLandinLineUpList') as $element)
$stripped = strip_tags($element);
$stripped = preg_replace('/\s+/', ' ', $stripped);
echo json_encode($stripped);
What I want
If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "$1", $content));
Try this
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '$1', $content);
}
return $content;
}
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
You don't need lookaround. Just go with
$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);
I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.
This worked for me.
$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg
In the figure above, router R1 has two point-to-point . ';
$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);
echo $parse_img;
Suyash
I need to extract the link value which is stored in a <a> tag by using php code.
I used below code
$url = '<a title="Question" href="http://stackoverflow.com/questions/ask">t</a>';
preg_match("/href=\"(.*?)\"/i", $url, $matches);
print_r($matches);
With the above code I can able to get the value of a single href. But it is not working for multiple href in a string(i.e $url = '<a title="Question" href="http://stackoverflow.com/questions/ask">t</a>x';). How can I achieve this?
Use a DOM parser, this example should get you going:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<a title="Question" href="http://stackoverflow.com/questions/ask">t</a>');
$elm = $doc->getElementsByTagName('a')->item(0);
foreach ($elm->attributes as $attr) {
echo $attr->name . ' ' . $attr->value . '<br>';
}
echo "Directly getting href: " . $elm->attributes->getNamedItem('href')->value;
Output:
title Question
href http://stackoverflow.com/questions/ask
Directly getting href: http://stackoverflow.com/questions/ask
Demo: http://viper-7.com/EN1Usi
Docs: http://php.net/manual/en/class.domdocument.php
Use preg_match_all() to get all the matches, not just the first.
$url = '<a title="Question" href="http://stackoverflow.com/questions/ask">t</a> x';
preg_match_all("/href=\"(.*?)\"/i", $url, $matches);
print_r($matches);
Here is my regex to get the image url on the page.
<?php
$url = $_POST['url'];
$data = file_get_contents($url);
$logo = get_logo($data);
function get_logo($html)
{
preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);
//echo "mactch : $matches[0][0]";
return $matches[0][0];
}
?>
Is there any thing missing in regex? for some of the url it does not give image url though they have image in it.
for example: http://www.milanart.in/
it does not give image on that page.
Please No dome. I could not use it.
<?php
$url = "http://www.milanart.in";
$data = file_get_contents($url);
$logo = get_logo($data);
function get_logo($html)
{
preg_match_all("/<img src=\"(.*?)\"/", $html, $matches);
return $matches[1][0];
}
echo 'logo path : '.$logo;
echo '<img src="'.$url.'/'.$logo.'" />';
?>
Use DOM Class of PHP to get all images:
Search for image files in CSS.....url(imagefilename.extension)
Search for image file in HTML ......