Jump to content

Help!! trying to set URL string data as variables


daveh33

Recommended Posts

Hi I could really use some help!!

 

I am trying to build a script that will read a list of strings from another webpage & break down the string to the info I need.

 

Example string on the webpage: -

 

2104&The+title&This+is+where+the+description+goes

 

so its basically ID&title&description with + as space

 

Can I use the $_GET command? or am I way off?

 

All I want to do to use the same example.. is to get the output

$id = 2104;

$title = "The title";

$description = "This is where the description goes";

 

ANY HELP IS MUCH APPRECIATED

Are you capturing a url from an external website and the url is formatted like so:

'id=ID&title=Title&description=Description

If so the following should work:

<?php

$url = 'id=2048&title=The+World&description=The+world+description+here';

$url_vars = explode('&', $url);

foreach($url_vars as $url_var)
{
   list($var, $value) = explode('=', $url_var);

   $$var = urldecode($value);
}

echo $id . '<br />' . $title . '<br />' . $description;

?>

OK now we are making progress! I have my php page and it displays the content from the external site -  and I have that script that you wrote functioning.

 

The missing gap is taking that data and adding it to the $url - I noticed in your code you used id=2048&title= with the =, that isn't part of my string - which makes it harder to access and I dont no how to do it.. any ideas??

Maybe:

<?php

$url_list = file_get_contents('list-of-urls.html');
$url_list = str_replace(array("\r\n", "\r", "\n"), "\n", $url_list);

$urls = explode("\n", $url_list);

foreach($urls as $url)
{
    list($id, $title, $description) = array_map('urldecode', explode('&', $url));

    echo '<p>' . $id . '<br />' . $title . '<br />' . $description . "</p>\n";
}

?>

If the link is like this:

 

http://site.com/page.php?id&title&desc

 

Try this:

 

$url = array_keys($_GET);
$url = explode('&', $url[0]);
$id = $url[0];
$title = $url[1];
$desc = $url[2];

 

If the link is like this:

 

http://site.com/page.php?id&title&desc

 

Try this:

 

$url = array_keys($_GET);
$url = explode('&', $url[0]);
$id = $url[0];
$title = $url[1];
$desc = $url[2];

 

$_GET will only work if the urls are coming from daveh33's website. daveh33 is getting them from an external webpage.

Maybe:

<?php

$url_list = file_get_contents('list-of-urls.html');
$url_list = str_replace(array("\r\n", "\r", "\n"), "\n", $url_list);

$urls = explode("\n", $url_list);

foreach($urls as $url)
{
    list($id, $title, $description) = array_map('urldecode', explode('&', $url));

    echo '<p>' . $id . '<br />' . $title . '<br />' . $description . "</p>\n";
}

?>

 

Thanks that works great!

 

The only thing - what variables do I use to insert into mysql? Would it be $url1, $url2 etc..?

 

Your status needs updating. Super Genius! ;DMuch appreciated

You'd do the query within the foreach loop after this line:

list($id, $title, $description) = array_map('urldecode', explode('&', $url));

 

Eg:

// connect to your database here

foreach($urls as $url)
{
    list($id, $title, $description) = array_map('urldecode', explode('&', $url));

    // add url info to database:
    $title = mysql_real_escape_string($title);
    $description = mysql_real_escape_string($description);

    // prepare query
    $sql = "INSERT INTO tbl_name (id, title, description) VALUES('$id', '$title', '$description')";
    // run query
    mysql_query($sql);
}

 

Change the example query so it works with your database.

Little mixed up.. would this  be the complete code

 

 

<?php
$url_list = file_get_contents('xxxx');
$url_list = str_replace(array("\r\n", "\r", "\n"), "\n", $url_list);
$urls = explode("\n", $url_list);
foreach($urls as $url)
{
    list($id, $title, $description) = array_map('urldecode', explode('&', $url));
    echo '<p>' . $id . '<br />' . $title . '<br />' . $description . "</p>\n";
}
// connect to your database here

foreach($urls as $url)
{
    list($id, $title, $description) = array_map('urldecode', explode('&', $url));

    // add url info to database:
    $title = mysql_real_escape_string($title);
    $description = mysql_real_escape_string($description);

    // prepare query
    $sql = "INSERT INTO tbl_name (id, title, description) VALUES('$id', '$title', '$description')";
    // run query
    mysql_query($sql);
}

?>

Only need to one foreach loop:

<?php
$url_list = file_get_contents('xxxx');
$url_list = str_replace(array("\r\n", "\r", "\n"), "\n", $url_list);
$urls = explode("\n", $url_list);

// connect to your database here

foreach($urls as $url)
{
    list($id, $title, $description) = array_map('urldecode', explode('&', $url));

    // add url info to database:
    $title = mysql_real_escape_string($title);
    $description = mysql_real_escape_string($description);

    // prepare query
    $sql = "INSERT INTO tbl_name (id, title, description) VALUES('$id', '$title', '$description')";
    // run query
    mysql_query($sql);
}

?>

Just a little something to complete it - I am looking to remove all characters except letters and numbers from the string.

 

I know I can do

 

$string = "This is some text and numbers 12345 and symbols !£$%^&";
$new_string = ereg_replace("[^A-Za-z0-9]", "", $string);

 

Would I just do that replacing url? And would it need on the line after the start of the loop?

Do you want to remove those characters from $title and $description?

 

Add the following lines:

    $title = ereg_replace("[^A-Za-z0-9]", "", $title);
    $description = ereg_replace("[^A-Za-z0-9]", "", $description);

 

Before the following:

    // add url info to database:
    $title = mysql_real_escape_string($title);
    $description = mysql_real_escape_string($description);

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.