How to capture href links and replace this via PHP - php

I have a php variable with some html content, and some href links. I need to capture this links, save to a DB and replace it with the id of the row that I just saved (is to track how many people follows that link in a newsletter application).
Basicatly I need to do the 2 functions in the example (some_function_to_save_the_links_to_array and some_function_to_save_the_links_to_array).
Thank you a lot for your help!
Example:
$var = "<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.google.com'>Some links</a> in the body of this <a href='http://www.yahoo.com'>Newsletter</a> and I want to extract their.</body></html>";
//Here I just no know how to do this, but I need to save http://www.google.com and http://www.yahoo.com to, maybe, an array, and save this array in a mysql db.
some_function_to_save_the_links_to_array;
while (THERE ARE VALUES IN THE ARRAY OF THE LINKS){
save $array['X'] to db //(I already know how to do it, this is not the problem)
$id = last inserted row in db //I know how to do it also
function_to_replace_the_links_for_the_id;
}
echo $var;
And this is the echo:
<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.mysite.com/link.php?id=1'>Some links</a> in the body of this <a href='http://www.mysite.com/link.php?id=1'>Newsletter</a> and I want to extract their.

<?php
function captureLink($content)
{
$links = array();
$pattern = "/<a\s+href=[\"\']([^>]+?)[\"\']/iU";
if(preg_match_all($pattern,$content,$matches)) {
for($i = 0;$link = $matches[$i][1];$i++)
array_push($links,$link);
}
return $links;
}
function insertLinksToDb(array $links)
{
$linksDb = array();
foreach($links as $link) {
$hash_link = md5($link);
$sql = "SELECT (id) FROM links WHERE hash LIKE :hash";
$sth = $dbh->prepare($sql);
$sth->bindValue(':hash',$hash_link,PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
if(!empty($result)) {
$id = $result['id'];
$linksDb[$id] = $link;
} else {
$sql = " INSERT INTO links (hash,link) VALUES(:hash,:link);";
$sth = $dbh->prepare($sql);
$sth->execute(array(':hash'=>$hash_link,':link',$link));
$linksDb[PDO::lastInsertId] = $link;
}
}
return $linksDb;
}
function normallizeLinks($content,array $links)
{
foreach($links as $id => $link) {
//str_replace working faster than strtr
$content = str_replace($link,'/links.php?id='.$id,$content);
}
return $content;
}

Related

How to loop through a JSON array and put data in variables

I am trying to get the data from an API and save it to a MySQL database. The problem is when I want to echo the data to check if it gets all the values I'm getting this error:
Notice: Trying to get property of non-object
My JSON data looks like this:
{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"},
I think this is because it is in an array. Because when I use this bit of code it gives no errors but I have to define the index of the array. What is the best way to loop through my data and put it in variables?
<?php
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));
//Fetch data
$attractieId = $data->AttractionInfo[0]->Id;
$attractieType = $data->AttractionInfo[0]->Type;
$attractieStatus = $data->AttractionInfo[0]->State;
$attractieStatusKleur = $data->AttractionInfo[0]->StateColor;
$attractieWachttijd = $data->AttractionInfo[0]->WaitingTime;
$attractieStatusPercentage = $data->AttractionInfo[0]->StatePercentage;
?>
I tried to find some solutions but all they use is a foreach loop. But i don't think that'll work right. Can anyone help me in the good direction or tell me how I might possibly fix this? I am not very experienced so any advice is welcome. Thanks in advance.
Update code:
require 'database-connection.php';
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));
//Fetch data
$attractieId = isset($data->AttractionInfo->Id);
$attractieType = isset($data->AttractionInfo->Type);
$attractieStatus = isset($data->AttractionInfo->State);
$attractieStatusKleur = isset($data->AttractionInfo->StateColor);
$attractieWachttijd = isset($data->AttractionInfo->WaitingTime);
$attractieStatusPercentage = isset($data->AttractionInfo->StatePercentage);
$sql = "INSERT INTO attracties (attractieId, attractieType, attractieStatus, attractieStatusKleur, attractieWachttijd, attractieStatusPercentage)
VALUES ('$attractieId', '$attractieType', '$attractieStatus', '$attractieStatusKleur', '$attractieWachttijd', '$attractieStatusPercentage')";
if ($db->query($sql) === TRUE) {
echo "success";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
It says 'success' but when I look into my database it inserted only empty data. I need to add all the attractions to my database so not just one row. So I need to loop through my data.
try this...
$content = json_decode(file_get_contents($url));
foreach($content->AttractionInfo as $data ){
$id = $data->Id;
$type = $data->Type;
$map = $data->MapLocation;
$state = $data->State;
$color = $data->StateColor;
if(!empty($data->WaitingTime)) {
$time = $data->WaitingTime;
}
if(!empty($data->StatePercentage)) {
$percent = $data->StatePercentage;
}
//persist your data into DB....
}
I think you need something like this:
$json = '{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"}]}';
$arr = json_decode($json);
foreach ($arr->AttractionInfo as $key => $attraction) {
foreach($attraction as $key=> $value) {
print_r($key.' - '.$value.'<br>');
$$key = $value;
}
}
echo '<br>';
echo $Id; // it will be last item/attraction id.
We can improve this code. Just say where and how do you want to use it

html php echo on div

here i have a simple function but this show me data fom sql only in 1 div i want to show [ on div 1 show 1 data, in other div show 2 data, etc etc]...
function load_post($added_by)
{
global $Connection;
$SQL_3 = mysqli_query($Connection, "SELECT * FROM posts WHERE added_by='$added_by'");
$NumPosts = mysqli_num_rows($SQL_3);
$out['num_posts'] = $NumPosts;
while($Fetch_3 = mysqli_fetch_array($SQL_3))
{
$out['id'] = $Fetch_3['id'];
$out['text'] = $Fetch_3['text'];
$out['added_by'] = $Fetch_3['added_by'];
$out['mp4'] = $Fetch_3['mp4'];
$out['likes'] = $Fetch_3['likes'];
$out['youtube'] = $Fetch_3['youtube'];
$out['image'] = $Fetch_3['image'];
$out['date_added'] = $Fetch_3['date_added'];
return $out;
}
}
index.php.
$posts = load_post('gentritabazi');
<div class="settings_forms_content">
<?php echo $posts['text']; ?>
</div>
return finish immediately your function so only one iteration is done. You need to save your results in array and then after loop return that array, sth like this:
$out = array();
while($Fetch_3 = mysqli_fetch_array($SQL_3))
{
$out[] = $Fetch_3;
}
return $out;
and display:
$posts = load_post('gentritabazi');
foreach ($posts as $post) {
echo '<div class="settings_forms_content">';
echo $post['text'];
echo '</div>';
}
Lots to amend, so I commented the code rather than write an essay
function load_post($con, $added_by)
{
// dont use globals, use parameters
//global $Connection;
// select only what you want to see not `*`
$SQL_3 = mysqli_query($con, "SELECT id, text, added_by, mp4,
likes, youtube, image, data_added
FROM posts
WHERE added_by='$added_by'");
// not needed
//$NumPosts = mysqli_num_rows($SQL_3);
//$out['num_posts'] = $NumPosts;
while($Fetch_3 = mysqli_fetch_array($SQL_3))
{
/*
* THsi code would overwrite the last iteration of the while loop
$out['id'] = $Fetch_3['id'];
$out['text'] = $Fetch_3['text'];
$out['added_by'] = $Fetch_3['added_by'];
$out['mp4'] = $Fetch_3['mp4'];
$out['likes'] = $Fetch_3['likes'];
$out['youtube'] = $Fetch_3['youtube'];
$out['image'] = $Fetch_3['image'];
$out['date_added'] = $Fetch_3['date_added'];
*/
// now as you SELECT only what you want yo can simply do
$out[] = $Fetch_3;
//return $out; instantly terminates the function
}
return $out; // return the array
}
Now call your function
// pass the connection as a parameter
$posts = load_post($Connection, 'gentritabazi');
// if you want to know how many results were returned
$result_count = count($posts);
// process the returned array
foreach ( $posts as $post ) {
echo '<div class="settings_forms_content">';
echo $post['text'];
echo '</div>';
}
Your script is at risk of SQL Injection Attack
Have a look at what happened to Little Bobby Tables Even
if you are escaping inputs, its not safe!
Use prepared statement and parameterized statements
$posts = load_post('gentritabazi');
foreach ($posts ['text'] as $postText){
echo "<div class='settings_forms_content'>$postText</div>";
}
first line calls your function which returns the array $posts
this array looks like:
$posts = array(
"id" => array of ids
"text" => array of texts
...
);
if you want to access the third text it would be like this:
$posts ['text'][3]
the foreach iterates to your $post array with index "text" -> which is also an array
every value in this array, $post['text'] will be referenced with $postText -> that means:
$post['text'][1] = $postText (first time looping through foreach-loop)
$post['text'][2] = $postText (secondtime looping through foreach-loop)
..
if you are familiar with loops, a foreach is just the short version for
for(var $i=0;$i<length($posts['text'];$i++){
echo "<div class='settings_forms_content'>$posts['text'][i]</div>";
}

Shuffle youtube IDs

am new to php and I was wondering how I could retrieve shuffled YouTube IDs..
Here is what I mean..
$playlist_id = "PLB9DAD6B9EDAEE7BC";
$cont = json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/'.$playlist_id.'/?v=2&alt=json&feature=plcp'));
$feed = $cont->feed->entry;
if(count($feed)) {
foreach($feed as $item) {
$title = $item->title->{'$t'};
$desc = $item->{'media$group'}->{'media$description'}->{'$t'};
$id = $item->{'media$group'}->{'yt$videoid'}->{'$t'};
}
}
This basically fetches the id, title and description from the playlist, how can I shuffle the $id to give me unique unrepeated values so that I can later on use it here?
<iframe ... src="http://www.youtube.com/embed/<?= $id ?>" allowfullscreen></iframe>
My goal is to refresh the page to get a new video each time I visit it and reset itself when it's over (or just continue picking unique values)
Thank you in advance, a lot..
You can store all the feed videos in an array and then use array_rand to get a random entry of the array.
See http://php.net/manual/de/function.array-rand.php for function reference.
Be aware that array_rand return a single key when used with default settings but it will deliver an array of keys if you choose to select more then a single random entry.
EDIT: Added cookie so the video is REAL random-unique
Code snippet:
$playlist_id = "PLB9DAD6B9EDAEE7BC";
$cont = json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/'.$playlist_id.'/?v=2&alt=json&feature=plcp'));
$feed = $cont->feed->entry;
$youtubeVideos = array();
if(count($feed))
{
foreach($feed as $item)
{
// build video array
$video = array();
$video['title'] = $item->title->{'$t'};
$video['desc'] = $item->{'media$group'}->{'media$description'}->{'$t'};
$video['id'] = $item->{'media$group'}->{'yt$videoid'}->{'$t'};
// push it into collection
$youtubeVideos[$video['id']] = $video;
}
}
$seenVideos=array();
$lastSeenVideo='';
// only get diff array if the cookies are set (= not first page view)
if(isset($_COOKIE['seen_youtube_videos']) && isset($_COOKIE['last_youtube_video']))
{
$lastSeenVideo=$_COOKIE['last_youtube_video'];
$seenVideos=unserialize($_COOKIE['seen_youtube_videos']);
$diffArr=$youtubeVideos;
foreach($seenVideos as $vidId)
unset($diffArr[$vidId]);
if(count($diffArr)>0)
{
// set difference for searching only
$youtubeVideos=$diffArr;
}
else
{
// if we did show all videos, reset everything
setcookie('seen_youtube_videos', '');
setcookie('last_youtube_video', '');
$seenVideos = array();
}
}
$randomizedKey = array_rand($youtubeVideos);
$randomVideo = $youtubeVideos[$randomizedKey];
do
{
$randomizedKey = array_rand($youtubeVideos);
$randomVideo = $youtubeVideos[$randomizedKey];
}
while($randomVideo['id'] == $lastSeenVideo);
$seenVideos[] = $randomVideo['id'];
setcookie('seen_youtube_videos', serialize($seenVideos));
setcookie('last_youtube_video', $randomVideo['id']);
// do stuff with $randomVideo

Pulling NHL Standings from XML Table with PHP

I'm working on a project in which I pull various statistics about the NHL and inserting them into an SQL table. Presently, I'm working on the scraping phase, and have found an XML parser that I've implemented, but I cannot for the life of me figure out how to pull information from it. The table can be found here -> http://www.tsn.ca/datafiles/XML/NHL/standings.xml.
The parser supposedly generates a multi-dimmensional array, and I'm simply trying to pull all the stats from the "info-teams" section, but I have no idea how to pull that information from the array. How would I go about pulling the number of wins Montreal has? (Solely as an example for the rest of the stats)
This is what the page currently looks like -> http://mattegener.me/school/standings.php
here's the code:
<?php
$strYourXML = "http://www.tsn.ca/datafiles/XML/NHL/standings.xml";
$fh = fopen($strYourXML, 'r');
$dummy = fgets($fh);
$contents = '';
while ($line = fgets($fh)) $contents.=$line;
fclose($fh);
$objXML = new xml2Array();
$arrOutput = $objXML->parse($contents);
print_r($arrOutput[0]); //This print outs the array.
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($this->arrOutput,$tag);
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this- >arrOutput)-1];
array_pop($this->arrOutput);
}
}
?>
add this search function to your class and play with this code
$objXML = new xml2Array();
$arrOutput = $objXML->parse($contents);
// first param is always 0
// second is 'children' unless you need info like last updated date
// third is which statistics category you want for example
// 6 => the array you want that has wins and losses
print_r($arrOutput[0]['children'][6]);
//using the search function if key NAME is Montreal in the whole array
//result will be montreals array
$search_result = $objXML->search($arrOutput, 'NAME', 'Montreal');
//first param is always 0
//second is key name
echo $search_result[0]['WINS'];
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, $this->search($subarray, $key, $value));
}
return $results;
}
Beware
this search function is case sensitive it needs modifications like match to
a percentage the key or value changing capital M in montreal to lowercase will be empty
Here is the code I sent you working in action. Pulling the data from the same link you are using also
http://sjsharktank.com/standings.php
I have actually used the same exact XML file for my own school project. I used DOM Document. The foreach loop would get the value of each attribute of team-standing and store the values. The code will clear the contents of the table standings and then re-insert the data. I guess you could do an update statement, but this assumes you never did any data entry into the table.
try {
$db = new PDO('sqlite:../../SharksDB/SharksDB');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}
$query = "DELETE FROM standings";
$result = $db->query($query);
$xmlDoc = new DOMDocument();
$xmlDoc->load('http://www.tsn.ca/datafiles/XML/NHL/standings.xml');
$searchNode = $xmlDoc->getElementsByTagName( "team-standing" );
foreach ($searchNode as $searchNode) {
$teamID = $searchNode->getAttribute('id');
$name = $searchNode->getAttribute('name');
$wins = $searchNode->getAttribute('wins');
$losses = $searchNode->getAttribute('losses');
$ot = $searchNode->getAttribute('overtime');
$points = $searchNode->getAttribute('points');
$goalsFor = $searchNode->getAttribute('goalsFor');
$goalsAgainst = $searchNode->getAttribute('goalsAgainst');
$confID = $searchNode->getAttribute('conf-id');
$divID = $searchNode->getAttribute('division-id');
$query = "INSERT INTO standings ('teamid','confid','divid','name','wins','losses','otl','pts','gf','ga')
VALUES ('$teamID','$confID','$divID','$name','$wins','$losses','$ot','$points','$goalsFor','$goalsAgainst')";
$result= $db->query($query);
}

jQuery tag suggestion

I'm try include this plugin on my site, it's working fine but I am having some problems with PHP.
When I write the PHP like this:
$default_tags = 'Avanture, Giorgio, Armani, Depeche, Mode, Pevanje, Francuska, usluživanje, Pravo, Menadžer, prodaje, Advokat';
if (!#$_SESSION['existing_tags']) {
$_SESSION['existing_tags'] = $default_tags;
}
$existing_tags = $_SESSION['existing_tags'];
$tags = split(' ', $default_tags);
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && #$_GET['tag']) {
$match = array();
foreach ($tags as $tag) {
if (stripos($tag, $_GET['tag']) === 0) {
$match[] = $tag;
}
}
echo json_encode($match);
}
exit;
}
it works fine, but when I try to get a result from the database I have problems.
I have tried:
$query = mysql_query("SELECT * FROM tags");
while($row = mysql_fetch_array($query)) {
$default_tags = ''.$row['keyz'].', ';
if (!#$_SESSION['existing_tags']) {
$_SESSION['existing_tags'] = $default_tags;
}
$existing_tags = $_SESSION['existing_tags'];
$tags = split(' ', $default_tags);
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && #$_GET['tag']) {
$match = array();
foreach ($tags as $tag) {
if (stripos($tag, $_GET['tag']) === 0) {
$match[] = $tag;
}
}
echo json_encode($match);
}
exit;
}
And this method is not working for me. Also, here is a screenshot from my database table tags. What is wrong with the above code?
Your problem is you keep overriding $default_tags variable over and over again.
At the end of your while loop, all you have is your last row with a comma at the end.
Basically you're not storing anything but the last row in that variable.
If you do the following you would have something similar what you're trying to do:
$default_tags_arr = array();
while($row = mysql_fetch_array($query)) {
array_push($default_tags_arr, $row["keyz"]);
}
$default_tags = join(", ",$default_tags_arr);
You should describe exactly what the problem is. Do you get an error message?
One thing I'm seeing that seems wrong to me is the fetch from the database:
while($row = mysql_fetch_array($query)) {
$default_tags = ''.$row['keyz'].', ';
For every row you are overriding $default_tags completely. I think maybe you wanted to say:
while($row = mysql_fetch_array($query)) //where does the curly brace closes in the original code?
$default_tags .= ''.$row['keyz'].', '; //notice the . before =
to concatenate the tags.
Aside from this I'm having trouble understanding the code:
$existing_tags = $_SESSION['existing_tags'];
$tags = split(' ', $default_tags);
Here you are assigning the variable $existing_tags which you don't use later in the code. Did you want to use it in the next line instead of $default_tags? What is the code supposed to do exactly?

Categories