I want to extract a "name" and an "ID" from a table which is loaded via a url. the table always contain different values.
One table could be this one: https://www.lectio.dk/lectio/262/FindSkema.aspx?type=stamklasse
i want to take every name and the id in the link its pointing to.
ex: name: 1m/id: 4514546468 (id from link: https://www.lectio.dk/lectio/262/SkemaNy.aspx?type=stamklasse&klasseid=4514546468)
how do i filter everything away but the things i need ? i get the source code with cURL and want to filter away the code so i have what i need left.
i did this whit a list with always fixed values, but since the table is always with different values i cant use the same code:
<?php
$ch = curl_init("http://www.lectio.dk/lectio/login_list.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
$output = curl_exec($ch); // do it!
curl_close($ch); // close handle
$expld = explode("<a", $output);
$list = array();
$remove = array("/", "d");
$removefrom = array("/", "'", ">", "<");
foreach($expld as $key){
$gymnr = substr($key, 15, 3);
$gymnr = str_replace($remove, "", $gymnr);
$gymname = substr($key, 54);
$gymname = str_replace($removefrom, "", $gymname);
$gymname = substr($gymname, 0, -40);
$prelist = array($gymname, $gymnr);
$gymlist[] = $prelist;
};
unset($gymlist[0]);
foreach($gymlist as $school){
echo "<li data-id='".$school[1]."'>".$school[0]."</li>";
}
?>
I think i can use regex to filter this, but i don't know enough about it, i need examples or solutions.
Related
How i can display the last numbers of a redirected url in php?
with redirected, i mean something like this
$nick=$_GET['nickname'];
$url='http://es.cheese.formice.com/mouse/' . $nick . '';
For example, if the url is script.php?nickname=Skyleter, will be http://es.cheese.formice.com/mouse/Skyleter, the problem here is it redirects to http://es.cheese.formice.com/mouse/Skyleter.17529827, (try yourself) so i want to display the numbers of the redirected url.
Wich, for me is 17529827
Is this possible?
Please dont say "the last 8 digits" like $variable = substr($url, -8); , so every nick has different ID.
Also ill like to display a custom error when input is value. [script.php and not script.php?nick=nick]
It displays "undefinex index" by default.
Thanks!
If it's always going to be numbers, you could use filter_var to easily pull them out.
$url = 'http://es.cheese.formice.com/mouse/Skyleter.17529827';
$id = filter_var($url, FILTER_SANITIZE_NUMBER_INT);
echo $id; // 17529827
If the nickname contains numbers though, this won't work. If the format stays that way (always ending with a dot and a set of numbers), then I guess you could use pathinfo and read that as the extension of your path.
$urlParts = pathinfo('http://es.cheese.formice.com/mouse/Skyleter1111.17529827');
echo $urlParts['extension'];
Since this url will redirect you, you will need to use curl to get the next one.
This will work:
<?php
$nick='Skyleter'; //$_GET['nickname'];
$url='http://es.cheese.formice.com/mouse/' . $nick;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
$l = trim($r[1]);
$user_id = end((explode('.', $l)));
echo $user_id;
?>
It explodes the string at the last dot (.), which is what comes right before the ID.
I'm not sure if what I want to do is possible but here's the case.
Cookies are set on server A (first name, last name, etc).
I have a script on server A which gets the cookies, saves it into the db for future use and then finally displays it. Let's say the script is getCookies.php
Here's the code:
include 'dbconnect.php';
$sessid = $_GET['sid'];
$un = $_COOKIE['un'];
$ul = $_COOKIE['ul'];
$up = $_COOKIE['up'];
$ue = $_COOKIE['ue'];
$idn = $_COOKIE['idn'];
if(!empty($un) || !empty($ul) || !empty($up) || !empty($ue) || !empty($idn)){ // Save log to Database
$savedate = date('Y-m-d G:i');
$q = "INSERT INTO cookiedb (sid, un, ul, up, ue, idn, savedate) VALUES ('$sessid', '$un', '$ul', '$up', '$ue', '$idn', '$savedate')";
$rs = mysqli_query($con,$q);
}
echo "$un, $ul, $up, $ue, $idn";
The code above works if I directly access the script from the browser. However, if I access on another server (server B) using CURL, the cookies doesn't seem to work. It's not being read and saved in the db. I'm getting a blank response. I even used some codes like this suggestion I found here on stackoverflow:
$url = "http://serverA.co.za/getCookie.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
// get cookie
// multi-cookie variant contributed by #Combuster in comments
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
var_dump($matches);
...but this code does not work. Do you have any idea how I can get the value for those cookies? If CURL could not be used here, are there any other ways? Thank you.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
I have this block of text (from the Discogs API) detailing information about bands that contain the word "Pink"...
http://pastebin.com/3vBnC0aE
I'm trying to figure out how to correctly extract the artist names from this block of text. My attempt was:
<?php
$url = "https://api.discogs.com/database/search?type=artist&q=pink"; // add the resource info to the url. Ex. releases/1
//initialize the session
$ch = curl_init();
//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'YourSite/0.1 +http://your-site-here.com');
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);
//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the curl session
$output = curl_exec($ch);
//close the session
curl_close ($ch);
function textParser($text, $css_block_name){
$end_pattern = '], "';
switch($css_block_name){
# Add your pattern here to grab any specific block of text
case 'title';
$end_pattern = '", "';
break;
}
# Name of the block to find
$needle = "\"{$css_block_name}\":";
# Find start position to grab text
$start_position = stripos($text, $needle) + strlen($needle);
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
$text_portion = str_ireplace("[", "", $text_portion);
$text_portion = str_ireplace("]", "", $text_portion);
return $text_portion;
}
$blockTitle = textParser($output, 'title');
echo $blockTitle. '<br/>';
?>
but that's giving this error:
Warning: stripos(): Offset not contained in string in C:\xampp\htdocs\WellItsFixed3\TTpage1.php on line 41
Line 41 is
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
The ultimate goal is to be able to present the extracted band titles in a list.
Any insight appreciated. Thank you.
This is clearly a JSON encoded string and you are overshooting with your approach. Just do:
$data = json_decode($your_string);
and $data will contain all the info in a structured way, see the json_decode() manual for more details.
Is it possible to pull text data from another domain (not currently owned) using php? If not any other method? I've tried using Iframes, and because my page is a mobile website things just don't look good. I'm trying to show a marine forecast for a specific area. Here is the link I'm trying to display.
Update...........
This is what I ended up using. Maybe it will help someone else. However I felt there was more than one right answer to my question.
<?php
$ch = curl_init("http://forecast.weather.gov/MapClick.php?lat=29.26034686&lon=-91.46038359&unit=0&lg=english&FcstType=text&TextType=1");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
?>
This works as I think you want it to, except it depends on the same format from the weather site (also that "Outlook" is displayed).
<?php
//define the URL of the resource
$url = 'http://forecast.weather.gov/MapClick.php?lat=29.26034686&lon=-91.46038359&unit=0&lg=english&FcstType=text&TextType=1';
//function from http://stackoverflow.com/questions/5696412/get-substring-between-two-strings-php
function getInnerSubstring($string, $boundstring, $trimit=false)
{
$res = false;
$bstart = strpos($string, $boundstring);
if($bstart >= 0)
{
$bend = strrpos($string, $boundstring);
if($bend >= 0 && $bend > $bstart)
{
$res = substr($string, $bstart+strlen($boundstring), $bend-$bstart-strlen($boundstring));
}
}
return $trimit ? trim($res) : $res;
}
//if the URL is reachable
if($source = file_get_contents($url))
{
$raw = strip_tags($source,'<hr>');
echo '<pre>'.substr(strstr(trim(getInnerSubstring($raw,"<hr>")),'Outlook'),7).'</pre>';
}
else{
echo 'Error';
}
?>
If you need any revisions, please comment.
Try using a user-agent as shown below. Then you can use simplexml to parse the contents and extract the text you want. For more info on simplexml.
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-agent: www.example.com"
)
);
$content = file_get_contents($url, false, stream_context_create($opts));
$xml = simplexml_load_string($content);
You may use cURL for that. Have a Look at http://www.php.net/manual/en/book.curl.php
I want to search number of links or URL on http://public-domain-content.com
and store them in an array and then just randomly select any one from array and just display or echo
How can i do that in php
If I understood what you're asking, you can achieve this using file_get_contents();
After using file_get_contents($url), which gives you a string, you can loop through the result string searching for spaces to tell the words apart. Count the number of words, and store the words in an array accordingly. Then just choose a random element from the array using array_rand()
However, sometimes there are security problems with file_get_contents().
You can override this using the following function:
function get_url_contents($url)
{
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
http://php.net/manual/en/function.curl-setopt.php <--- Explanation about curl
Example code:
$url = "http://www.xxxxx.xxx"; //Set the website you want to get content from
$str = file_get_contents($url); //Get the contents of the website
$built_str = ""; //This string will hold the valid URLs
$strarr = explode(" ", $str); //Explode string into array(every space a new element)
for ($i = 0; $i < count($strarr); $i++) //Start looping through the array
{
$current = #parse_url($strarr[$i]) //Attempt to parse the current element of the array
if ($current) //If parse_url() returned true(URL is valid)
{
$built_str .= $current . " "; //Add the valid URL to the new string with " "
}
else
{
//URL invalid. Do something here
}
}
$built_arr = explode(" ", $built_str) //Same as we did with $str_arr. This is why we added a space to $built_str every time the URL was valid. So we could use it now to split the string into an array
echo $built_arr[array_rand($built_arr)]; // Display a random element from our built array
There is also a more extended version to checking URLs, which you can explore here:
http://forums.digitalpoint.com/showthread.php?t=326016
Good luck.