How can I get just the URL from this? - php

I get a lot of DMCA removal emails for my website, and I'm trying to automate the process of removing those tracks from my website.
The emails all come looking similar to this.
http://example.com/title-to-post.html title - to post
http://example.com/title-of-post.html title - of post
http://example.com/some-song-artist-some-song-name.html some song artist - some song name
But there's a lot of them, I only wanna return the URL portion of every part of this, example being below.
http://example.com/title-to-post.html
http://example.com/title-of-post.html
http://example.com/some-song-artist-some-song-name.html
EDIT: I am storing these files into a txt file, then calling them using standard code.
$urls = file_get_contents( "oururls.txt" );
$data = explode(",", $urls);
foreach ($data as $item) {
echo "$item";
echo '<br>';
}
Nothing really fancy going on, how ever it's returning the title also and I want just the urls.

If there's always a space after the URL you can explode the text by " " and get the first portion. Example:
$example = explode(" ", $url);
echo $example[0];

Related

Get info from API/URL

I have the URL https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680
which outputs statistics.
For example:
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
I want to get the value of link count.
Can anyone help?
I tried to do:
<?php
$content = file_get_contents("https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680");
$result = json_decode($content);
print_r( $result->link );
?>
But it didn't work.
Put the JSON in an editor and you'll see that it's an array and not an object with the link attribute. This is why you cannot access it directly. You have to loop over the items and then you'll be able to access the link property of one of the items. If you need to access the link by id, as you asked 4 months later, then just create a dictionnary in an array indexed by id and containing just the interesting data you need.
PHP code:
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
echo '$items = ' . var_export($items, true) . "\n\n";
// Create a dictionnary to store each link accessible by id.
$links_by_id = [];
// Loop over all items:
foreach ($items as $i => $item) {
// Show how to access the current link.
echo "Link $i = $item->link\n";
// Fill the dictionary.
$links_by_id[(int)$item->id] = $item->link;
}
// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";
// Example of access by id:
// The id seems to be a string. It could probably be "1895" or "zhb34" or whatever.
// (If they are only numbers, we could convert the string to an integer).
$id = "1859";
echo "\nAccess with id $id = " . $links_by_id[$id] . "\n";
Test it here: https://onlinephp.io/c/e8ab9
Another important point: You are getting a 403 Forbidden error on the URL you provided. So typically, you will not obtain the JSON you wanted.
As I explained in the comment below, I think that you will not be able to access this page without having a fresh URL with valid query parameters and/or cookies. I imagine you obtained this URL from somewhere and it is no longer valid. This is why you'll probably need to use cURL to visit the website with a session to obtain the fresh URL to the JSON API. Use Google to find some examples of PHP scraping/crawling with session handling. You'll see that depending on the website it can get rather tricky, especially if some JavaScript comes into the game.

Trying to grab value from html page but getting template back not the value - php

I am making a price crawler for a project but am running into a bit of an issue. I am using the below code to extract values from an html page:
$content = file_get_contents($_POST['url']);
$resultsArray = array();
$sqlresult = array();
$priceElement = explode( '<div>value I want to extract</div>' , $content );
Now when I use this to get certain elements I only get back
Finance: {{value * value2}}
I want to get the actual value that would be displayed on the screen e.g
Finance: 7.96
The other php methods I have tried are:
curl
file_get_html(using simple_html_dom library)
None of these work either :( Any ideas what I can do?
You just set the <div>value I want to extract</div> as a delimiter, which means PHP looks for it to separate your string to array whenever this occurs.
In the following code we use , character as a delimiter:
<?php
$string = "apple,banana,lemon";
$array = explode(',', $string);
echo $array[1];
?>
The output should be this:
banana
In your example you set the value you want to extract as a delimiter. That's why this happens to you. You'll need to set a delimiter between your string you want to obtain and other string you won't need at the moment.
For example:
<?php
$string = "iDontNeedThis-dontExtractNow-value I want to extract-dontNeedEither";
$priceElement = explode('-', $string);
echo "<div>".$priceElement[2]."</div>";
?>
The code should output this to your HTML page:
<div>value I want to extract</div>
And it will appear on your page like this:
value I want to extract
If you don't need to save the whole array in a variable, you can save the one index of it to variable instead:
$priceElement = explode('-', $string)[2];
echo $priceElement;
This will save only value I want to extract so you won't have to deal with arrays later on.

PHP Nested Starcraft 2 Ladder Api

I want to make a personal profile page for my Starcraft 2 Clan with the API in PHP.
The normal stats are working for me.
$json = file_get_contents('http://eu.battle.net/api/sc2/profile/3077083/1/gbot/');
$obj = json_decode($json);
echo $obj->displayName;
However when I'll want to use the ladder stats I can't even display one variable.
$json = file_get_contents('
http://eu.battle.net/api/sc2/profile/3077083/1/gbot/ladders?locale=en_GB');
$lad = json_decode($json);
So how can I display the stats from the child with HOTS_SOLO in it?
This is just basic array access?
$json = file_get_contents('http://eu.battle.net/api/sc2/profile/3077083/1/gbot/ladders?locale=en_GB');
$data= json_decode($json);
$currentSeason = $data->currentSeason;
foreach ($currentSeason as $obj) {
foreach ($obj->ladder as $ladder) {
if ($ladder->matchMakingQueue == 'HOTS_SOLO') {
// this is the ladder we want to display
echo $ladder->ladderName; // Tychus Theta
}
}
}
There appears to be a newline in your URL (it starts on one line, where the whole literal begins on the line before). The file_get_contents() may be failing.
If that's not the problem, then it's something more subtle. Firefox/Chrome don't seem to have a problem with it. If json_decode is choking, it might be a forgiveable syntax issue. Try saving the data locally, and then removing components until it parses, and see if you can then do a string-replace or something to fix it, going forward.

Replace certain content in txt file and save it using PHP?

So I receive variable replace it in certain area in txt file and save it back. I get page number and according to it I get exploded data. Anyway I'll post a code below to make it more clear:
$pgnm = $_GET['page']; //This is the page number as I've said.
$conts = file_get_contents("content.txt");
the content of content.txt looks like this:
text1|text2|text3
I display this content in certain pages. For example on first page: text1, on second text2, etc.
Now i'm working on a form where I successfully change these. I get as I've said page number and text:
$text = "new text"; //this is the content which I want to be replaced instead of text2.
I make the content.txt file look like this after its saved: text1|new text|text2
So lets go on:
$exp = explode("|", $conts); //this explodes data into slashes.
$rep = str_replace($exp[$pgnm], $text, $conts);
file_put_contents("content.txt", $rep); // Saving file
All these above-mentioned operations work perfectly, but here's my problem now. This only works if content.txt has certain content, if it's empty it enters my new text and that's all: 'new text' and that's all. Maybe I want to add second page content 'new text2' and after I finish entering it and save I want the file to be displayed like this: new text|new text2. If the content of content.txt looks like this: 'new text|' str_replace doesn't replace empty string. So that's my another problem too.
I tried everything, but couldn't manage to anything about this two problems. Thank you in advance for your help!
Why don't you use your $exp array for building the content. I mean $exp contains all the blocks as an array() one by one. So you just change, or add new values to the array (no str_replace() needed). Then rebuild using implode('|',$exp);.
As for your code;
$exp = explode("|", $conts); //this explodes data into slashes.
$exp[$pgnm] = $text;
file_put_contents("content.txt", implode('|',$exp)); // Saving file
Instead of str_replace use this code:
$pgnm = 1;
$text = 'new text';
$conts = 'text1||text3';
$exp = explode('|', $conts);
$exp[$pgnm] = $text;
$rep = implode('|', $exp);
var_dump($rep); // string(20) "text1|new text|text3"

PHP For Loop str_replace emoticons

I'm pretty new to PHP so please bear with me for this one.
I have an array with emoticons, and I want to replace the emoticon text with the correct image, all within a for loop. So I'm trying to take my text variable and do a str_replace, but I'm not sure exactly how to display the text after the emoticons have been changed.
Here is my code:
$content = ":D Here is a sample sentence for this example :)";
$emotes = array(
[":)","<img class='emoticon' src='smile.png'>"],
[":D","<img class='emoticon' src='grin.png'>"],
);
for($i=0;$i<count($emotes);$i++) {
$contentWithEmotes = str_replace($emotes[$i][0], $emotes[$i][1], $content);
}
print $contentWithEmotes;
The problem this this is that it only displays the last image from the array, when I want it to display both of them.
How should I go about displaying the content with the correct image?
Thanks in advance for any help.
Restructure your array like this:
$emotes = [
":)"=>"<img class='emoticon' src='smile.png' />",
":D"=>"<img class='emoticon' src=grin.png' />"
];
Then use strtr:
$contentWithEmotes = strtr($content,$emotes);
Each time through the loop you need to process the result of the previous time, not the original content.
$contentWithEmotes = $content;
foreach ($emotes as $emote) {
$contentWithEmotes = str_replace($emote[0], $emote[1], $contentWithEmotes);
}
However, the strtr() solution is better.

Categories