doa24uk Posted October 1, 2009 Share Posted October 1, 2009 Hi guys, I've used this method before but only on a plain $_POST string rather than an array. Here's my code, why is it returning nothing? What this does is take the multi-line values from a textarea, split them at the newline character \n then place them in an array. eg. http://site1.com http://site2.com And what I need to get ( which is why the str_replace & explode is in there) is --> site1.com site2.com // Takes value from the textarea & splits into individual array elements at the newline character $textareaname = $_POST['links']; $str=explode("\n", $textareaname); // While we're looping through the textarea lines, do the following for($i = 0; $i < count($str); $i++){ $url = $str[$i]; // Attempt at getting 'site1.com' from something like 'http://site1.com/folder/fgsjosdj2/index.html' echo $url . "<br><br>"; //Extracts the domain from a URL echo get_domain( $url ) . '<br />'; function get_domain( $url ) { if( strpos( $url, '://' ) !== false ) { list( $throwAway, $url ) = explode( '://', $url ); } if( strpos( $url, '/' ) !== false ) { $url = explode( '/', $url ); $url = array_shift( $url ); } if( preg_match( '/^www[^.]*\..*/', $url ) ) { $url = explode( '.', $url ); array_shift( $url ); $url = implode( '.', $url ); } Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/ Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 I'm not sure I understand. Your description sounds like you want this? <?php $textareaname = str_replace("http://", "", $_POST['links']); $str = explode("\n", $textareaname); foreach($str as $url) { echo $url . '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928442 Share on other sites More sharing options...
thebadbad Posted October 1, 2009 Share Posted October 1, 2009 You want to end up with a string again? You can use parse_url() to get the domain: <?php $array = explode(PHP_EOL, $_POST['links']); foreach ($array as &$url) { $url = parse_url($url, PHP_URL_HOST); } unset($url); echo implode(PHP_EOL, $array); ?> Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928451 Share on other sites More sharing options...
doa24uk Posted October 1, 2009 Author Share Posted October 1, 2009 Hi guys, badbad's code didn't work, cags' did but I need both the original URLs (broken by newline) and the domain only as two seperate variables. I then need to put them into the DB. I can use two of cags' codes to get the relevant parts but I can't deploy them in a single loop... Eg. Text from textarea http://site1.com/mylink.html\nhttp://site2.com/newlink Required output http://site1.com/mylink.html & site1.com --> INSERT INTO DB http://site2.com/newlink & site2.com --> INSERT INTO DB Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928540 Share on other sites More sharing options...
lemmin Posted October 1, 2009 Share Posted October 1, 2009 What about it is not working? This should work: <?php $urls = explode("\n", $_POST['links']); $domains = array(); foreach ($urls as $url) { preg_match("/http:\/\/(.+?)\//", $url, $matches); $domains[] = $matches[1]; } ?> If it doesn't, the input is probably not what you are expecting. Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928546 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 <?php $text = "http://site1.com/mylink.html\nhttp://site2.com/newlink"; $str = explode("\n", $text); foreach($str as $k=>$v) { $sql = "INSERT INTO `table` (`url`, `domain`) VALUES ('".$v."','".parse_url($v, PHP_URL_HOST)."')"; echo $sql . '<br/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928547 Share on other sites More sharing options...
thebadbad Posted October 1, 2009 Share Posted October 1, 2009 Gotta love those "didn't work" replies without any further details. Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928549 Share on other sites More sharing options...
doa24uk Posted October 1, 2009 Author Share Posted October 1, 2009 Sorry guys, Both solutions did work, it was just me being an idiot. Cags' second solution is working perfectly. Thank you very much all. Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928551 Share on other sites More sharing options...
doa24uk Posted October 1, 2009 Author Share Posted October 1, 2009 Just noticed a problem. It's adding an underscore onto the end of the parse_url variables eg. http://1.com http://2.com Gives 1.com_ 2.com_ Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928563 Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 Try echo'ing out the urlencode of $_POST['links'] like so. <?php echo urlencode($_POST['bob']); ?> I imagine then problem is that the rows are seperate by \r\n not just \n, so the whitespace character is being converted to an underscore. If thats the case changing the explode function to split on "\r\n" instead of on "\n" should solve the problem. Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928567 Share on other sites More sharing options...
doa24uk Posted October 1, 2009 Author Share Posted October 1, 2009 \r\n that was my problem .... def. all sorted now!!! Damn I love this forum!!! Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928570 Share on other sites More sharing options...
thebadbad Posted October 1, 2009 Share Posted October 1, 2009 Guess why I split the string on the PHP_EOL constant? Link to comment https://forums.phpfreaks.com/topic/176180-solved-explode-inside-array/#findComment-928581 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.