Jump to content

Recommended Posts

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

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);
?>

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

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.

<?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/>';
}
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.