daveh33 Posted October 3, 2007 Share Posted October 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted October 3, 2007 Share Posted October 3, 2007 See if this helps. http://www.php.net/manual/en/function.urlencode.php Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 Thanks for that - What is the best way of doing this as I need 2 first get the strings from another webpage? Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted October 3, 2007 Share Posted October 3, 2007 you can call them directly $_GET['ID'] $_GET['title'] $_GET['description'] Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 How is that possible? I am trying to assign those tags to the different parts of the string, seperated by &.. Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted October 3, 2007 Share Posted October 3, 2007 Okay, I'm a little unclear now. Are you trying to access the GET information that was sent to the current page, or are you trying to send GET information to another page? Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 OK I have a list of similar strings from a php generated page on a different web server - I want to get all of the strings - split it into ID, title & description and add them into a mysql database - any ideas?? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 Ah thats brilliant - but I need to define $url - its an actual webpage where there is a list of strings - its not physically a URL, I want to add it to the mysql to reform a URL Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 If its an external webpage then look in to file_get_contents to get the (html) source code. Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 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?? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 What is your string then? Post an example of the extension webpage/site you are linking to, to get the 'urls' Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 13336&LDrch&bln+bg+Bl+Asn but there is sometimes up to 20 different ones -I need to get all of them.. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted October 3, 2007 Share Posted October 3, 2007 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]; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
marcus Posted October 3, 2007 Share Posted October 3, 2007 Ah, ignore my post then Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 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); } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 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); } ?> Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 Brilliant I have that working - just need to make a few tweaks and my script will be complete. Thanks for your help Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 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? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 3, 2007 Share Posted October 3, 2007 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); Quote Link to comment Share on other sites More sharing options...
daveh33 Posted October 3, 2007 Author Share Posted October 3, 2007 Yes that is what I am trying to do - that works fine , but it also removes all of the spaces between words in the description.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.