paymentstv Posted July 17, 2011 Share Posted July 17, 2011 Hello All, I have a list of URLs submitted by users. I want to make sure: list has no White-spacing, and it contains only host values i.e site.com,subdomain.site.com,site2.com,site3.net I have the following code at the moment var list = document.getElementById( "list" ).value ; Taking out the white spaces in list $vars = array_map('trim', explode(',', $list)); is $list ok here? I am a newb and not sure the diff b/w $ and var making sure list only contains host values of the intended URLs (from php manual) function getHost($list) { $parseUrl = parse_url(trim($list)); return trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/', $parseUrl[path], 2))); } This would only work for one address at a time, can some one help me to make it work for the whole list? Need to iterate this method through the comma separated list Appreciate all your help. Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/ Share on other sites More sharing options...
premiso Posted July 17, 2011 Share Posted July 17, 2011 $list = explode(',', $vars); foreach ($list as $key => $host) { $list[$key] = getHost($host); } var_dump($list); Should do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243789 Share on other sites More sharing options...
paymentstv Posted July 17, 2011 Author Share Posted July 17, 2011 Hi, Thanks for the reply and help. It was pointed out to me that var list = document.getElementById( "list" ).value ; is Javascript when I try the php code it just won't even submit the form. This is what i have so far. function startProcess() { var channelname = document.getElementById( "channelname" ).value ; var sitename = document.getElementById( "sitename" ).value ; //sending private url example.com or null var privateurl = document.getElementById( "privateurl" ).value ; //?sending privateURL check 1 or 0? var privateurlcheck = document.getElementById( "privateurlcheck" ) ; //no value with nothing , checked always 0, with value it is 0 //sending new values if (privateurlcheck.checked == true) { var privateurlcheck = 1; // do something } else { var privateurlcheck = 0;} $vars = array_map('getHost', explode(',', $privateurl)); http.open('get', "ajax/createchannel.php?channel=" + channelname + "&sitename=" + sitename + "&privateurl=" + privateurl + "&privateurlcheck=" + privateurlcheck); http.onreadystatechange = handleProcessResponse; http.send(null); } function getHost($url) { $parseUrl = parse_url(trim($url)); return trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/', $parseUrl[path], 2))); } Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243808 Share on other sites More sharing options...
wildteen88 Posted July 17, 2011 Share Posted July 17, 2011 You're setting a javascript variable and expecting to use it within your PHP code!. You cannot mix Javascript with PHP, they are completely different languages. Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243814 Share on other sites More sharing options...
paymentstv Posted July 17, 2011 Author Share Posted July 17, 2011 Thanks for pointing that out. I realised this and now focusing on doing this on JavaScript var list = document.getElementById( "list" ).value ; var host = getHostname(str); function getHostname(str) { var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im'); return str.match(re)[1].toString(); } I think above will just get rid of ftp://, http://, and https:// prefixe but nothing more. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243820 Share on other sites More sharing options...
wildteen88 Posted July 17, 2011 Share Posted July 17, 2011 What do you need help with Javascript or PHP? This is a PHP forum for PHP code only. Javascript support is provided in the Javascript forum. Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243821 Share on other sites More sharing options...
paymentstv Posted July 17, 2011 Author Share Posted July 17, 2011 I think I have to do this using JavaScript since my script uses JavaScript before sending the list variable to database. Could some one move the thread? Sorry for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243822 Share on other sites More sharing options...
paymentstv Posted July 17, 2011 Author Share Posted July 17, 2011 Hello Again to all the JavaScript pros. I've been moved here from php! I have the following code and I want to make sure the [var privateurl] only contains comma separated host list Originally when clients submit privateurl it would be a list with full urls and white space i.e http://www.phpfreaks.com, domain.com/index.php, sub3.domain2.com, site3.com I want the list to be phpfreaks.com,domain.com,sub3.domain2.com,site3.com so that no white spaces and only host values of urls are sent to database. var channelname = document.getElementById( "channelname" ).value ; var sitename = document.getElementById( "sitename" ).value ; //sending private url example.com or null var privateurl = document.getElementById( "privateurl" ).value ; //?sending privateURL check 1 or 0? var privateurlcheck = document.getElementById( "privateurlcheck" ) ; //no value with nothing , checked always 0, with value it is 0 //sending new values if (privateurlcheck.checked == true) { var privateurlcheck = 1; // do something } else { var privateurlcheck = 0;} $vars = array_map('getHost', explode(',', $privateurl)); http.open('get', "ajax/createchannel.php?channel=" + channelname + "&sitename=" + sitename + "&privateurl=" + privateurl + "&privateurlcheck=" + privateurlcheck); http.onreadystatechange = handleProcessResponse; http.send(null); } I have found teh following code function getHostname(str) { var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im'); return str.match(re)[1].toString(); } but was told it will only get ride of http, https, ftp Can some one please help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/242197-getting-the-host-urls-from-a-list-of-urls-submitted/#findComment-1243830 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.