Jump to content

extracting url


w1ww

Recommended Posts

Hello :)

Well I'm new to the PHP 'world' and I'm trying to create an PHP script that extracts an url from this link. For example :

http://news.google.com/news/url?sa=T&ct=pt/17-0&fd=R&url=http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm&cid=1103238007&ei=8mmARcKVMb7maOid1dIO

So the script should extract what is between "url=" and the "&cid..." that in this case is http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm

I don't know if it is easy or hard to do .. But well, just asking!

Thank you!
Link to comment
https://forums.phpfreaks.com/topic/30552-extracting-url/
Share on other sites

If you are processing the URL from a string, and not your current page... eg if you are processing the news.google.com string, it's obvious that you aren't google.com so you'd want to look at the function outlined here...
http://simon.net.nz/articles/process-a-url-query-string-in-php/
Link to comment
https://forums.phpfreaks.com/topic/30552-extracting-url/#findComment-140660
Share on other sites

Try:
[code]

<?php

$url = "http://news.google.com/news/url?sa=T&ct=pt/17-0&fd=R&url=http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm&cid=1103238007&ei=8mmARcKVMb7maOid1dIO";

preg_match('/&url=(.*?)&cid=/', $url, $result);

echo $result[1]; // http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm

?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/30552-extracting-url/#findComment-140704
Share on other sites

You can also use a combination of the [url=http://www.php.net/parse_url]parse_url()[/url] and [url=http://www.php.net/parse_str]parse_str()[/url] functions:

[code]<?php
$url =
"http://news.google.com/news/url?sa=T&ct=pt/17-0&fd=R&url=http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm&cid=1103238007&ei=8mmARcKVMb7maOid1dIO";
$p_url = parse_url($url);
echo '<pre>' . print_r($p_url,true) . '</pre>';
parse_str($p_url['query'],$p_query);
echo '<pre>' . print_r($p_query,true) . '</pre>';
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/30552-extracting-url/#findComment-140713
Share on other sites

Here I am again .. I modified the code to this (which gives an error) : "Warning: implode() [function.implode]: Bad arguments. in /home/ricardoq/public_html/lastnews/rss/getrss.php on line 20"

This code gets the RSS feeds . In this case its for google!

I'm new to PHP so probably I made a lot of erros changing the code!
This code gets the RSS feeds . In this case its for google!

[code]
define("_NO_SESSION_",1);
require_once("config.inc.php");

$_count = $_GET["count"] ? $_GET["count"] : 10;
$_items = getMyItems();
for($nIndex = 0; $nIndex < count($_xml_sources); $nIndex++)
{
if($_xml_sources[$nIndex]["use"])
{
$_curr_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
if($_curr_time >= $_xml_sources[$nIndex]["next"])
{

$preurl = $_xml_sources[$nIndex];
preg_match('/&url=(.*?)&cid=/', $url, $newurl);
$_url = $newurl[1]["url"];

$_file = _APP_PATH_ . "cache/" . md5($newurl[1]["url"]);
$_content = implode("",file($_url));
$f = @fopen($_file,"w");
@fwrite($f,$_content);
@fclose($f);
$_url = $_file;
$_diff = $_xml_sources[$nIndex]["next"] - $_xml_sources[$nIndex]["last"];
$_xml_sources[$nIndex]["last"] = $_xml_sources[$nIndex]["next"];
$_xml_sources[$nIndex]["next"] += $_diff;
write_sources();
}
else
{
$_file = _APP_PATH_ . "cache/" . md5($_xml_sources[$nIndex]["url"]);
if(!file_exists($_file))
{
@mkdir(_APP_PATH_ . "cache/");
$_content = implode("",file($_xml_sources[$nIndex]["url"]));
$f = @fopen($_file,"w");
@fwrite($f,$_content);
@fclose($f);
}
$_url = $_file;
}
$_tmp = parseXML($_url);
}
if(count($_xml_data))
$_xml_data = array_merge($_xml_data,$_tmp);
else
$_xml_data = $_tmp;
}
$_xml_data = noDuplicates($_xml_data);
shuffle($_xml_data);
for($nIndex = 0; $nIndex < $_count - count($_items); $nIndex++)
$_all_items[] = $_xml_data[$nIndex];
shuffle($_all_items);
$_all_items = array_merge($_all_items,$_items);
$_output = implode("",file(_APP_PATH_ . "templates/output.inc.php"));
$_content = getContentParts($_output);
$_print = $_content["before"];
for($nIndex = 0; $nIndex < count($_all_items); $nIndex++)
{
$_tmp = $_content["item"];
$_tmp = str_replace("{TITLE}",$_all_items[$nIndex]["title"],$_tmp);
$_tmp = str_replace("{URL}",$_all_items[$nIndex]["url"],$_tmp);
$_tmp = str_replace("{DESCRIPTION}",$_all_items[$nIndex]["description"],$_tmp);
$_print .= $_tmp;
}
$_print .= $_content["after"];
echo $_print;
exit;
[/code]



[b]This is the original code: [/b]

[code]

define("_NO_SESSION_",1);
require_once("config.inc.php");

$_count = $_GET["count"] ? $_GET["count"] : 10;
$_items = getMyItems();
for($nIndex = 0; $nIndex < count($_xml_sources); $nIndex++)
{
if($_xml_sources[$nIndex]["use"])
{
$_curr_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
if($_curr_time >= $_xml_sources[$nIndex]["next"])
{
$_url = $_xml_sources[$nIndex]["url"];
$_file = _APP_PATH_ . "cache/" . md5($_xml_sources[$nIndex]["url"]);
$_content = implode("",file($_url));
$f = @fopen($_file,"w");
@fwrite($f,$_content);
@fclose($f);
$_url = $_file;
$_diff = $_xml_sources[$nIndex]["next"] - $_xml_sources[$nIndex]["last"];
$_xml_sources[$nIndex]["last"] = $_xml_sources[$nIndex]["next"];
$_xml_sources[$nIndex]["next"] += $_diff;
write_sources();
}
else
{
$_file = _APP_PATH_ . "cache/" . md5($_xml_sources[$nIndex]["url"]);
if(!file_exists($_file))
{
@mkdir(_APP_PATH_ . "cache/");
$_content = implode("",file($_xml_sources[$nIndex]["url"]));
$f = @fopen($_file,"w");
@fwrite($f,$_content);
@fclose($f);
}
$_url = $_file;
}
$_tmp = parseXML($_url);
}
if(count($_xml_data))
$_xml_data = array_merge($_xml_data,$_tmp);
else
$_xml_data = $_tmp;
}
$_xml_data = noDuplicates($_xml_data);
shuffle($_xml_data);
for($nIndex = 0; $nIndex < $_count - count($_items); $nIndex++)
$_all_items[] = $_xml_data[$nIndex];
shuffle($_all_items);
$_all_items = array_merge($_all_items,$_items);
$_output = implode("",file(_APP_PATH_ . "templates/output.inc.php"));
$_content = getContentParts($_output);
$_print = $_content["before"];
for($nIndex = 0; $nIndex < count($_all_items); $nIndex++)
{
$_tmp = $_content["item"];
$_tmp = str_replace("{TITLE}",$_all_items[$nIndex]["title"],$_tmp);
$_tmp = str_replace("{URL}",$_all_items[$nIndex]["url"],$_tmp);
$_tmp = str_replace("{DESCRIPTION}",$_all_items[$nIndex]["description"],$_tmp);
$_print .= $_tmp;
}
$_print .= $_content["after"];
echo $_print;
exit;

[/code]

Well, I think is asking too much for someone to check the code but :\ I'm stuck on this!

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/30552-extracting-url/#findComment-140723
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.