[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

reorder matched text file from master textfile part 2

Posted on 2004-10-27
3
Medium Priority
?
268 Views
Last Modified: 2009-12-16
hi there

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21185604.html       

Answer PART B if  question PART A works CORRECTLY.

Would it be possible to correct a url IF the second text file has a wrong url

master

http://www.domain1.com/master?=345
http://domain2.com/master?=hdf
http://domain3.com/correct?=fds
http://domain4.com/?=rwe
http://domain5.com=234
http://www.domain6.com=342

match file with right domain with wrong url format but right ID at end.

http://www.domain1.com/wrongtype?=234
http://domain3.com/correct?=365
http://domain4.com/oopps?=856
http://domain2.com/master?=465

CSV output with ,

Column A                                                             Column B

http://www.domain1.com/master?=345                  http://www.domain1.com/master?=234
http://domain2.com/master?=hdf                            http://domain2.com/master?=465
http://domain3.com/correct?=fds                            http://domain3.com/correct?=365
http://domain4.com/?=rwe                                    http://www.domain4=856
http://domain5.com=234                                       http://domain5.com=234
http://www.domain6.com=342                               http://www.domain6.com=342

thanks



0
Comment
Question by:playstat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 1000 total points
ID: 12431271
The first example actually does the following:
Match files which domain match, url do not necesarily match, and id matches:

<?php
$aMaster = file('master.txt');
$aSecond = file('second.txt');

$aMatch = array();

foreach($aSecond as $iIndex => $sUrl)
{
  //replace http://www.domain1.com/wrongtype?=234 into http://www.domain1.com=234
  $sUrl = trim($sUrl);
  if(($iEq = strpos($sUrl, "=")) !== false && ($iSlash=strpos($sUrl,'/',7)) !== false)
  {
    $sUrlShort = substr($sUrl,0,$iSlash).substr($sUrl,$iEq);
  }
  else
    $sUrlShort = $sUrl;
 
  $aSecond[$iIndex] = array('u' => trim($sUrl), 's' => trim($sUrlShort));
}

foreach($aMaster as $sUrl)
{
  $sUrl = trim($sUrl);
  if(($iEq = strpos($sUrl, "=")) !== false && ($iSlash=strpos($sUrl,'/',7)) !== false)
  {
    $sUrlShort = substr($sUrl,0,$iSlash).substr($sUrl,$iEq);
  }
  else
    $sUrlShort = $sUrl;

  foreach($aSecond as $iSIndex => $aUrl)
  {
    if(strpos($sUrlShort, $aUrl['s']) !== false)
    {
      $aMatch[] = array($sUrl, $aUrl['u']);
      unset($aSecond[$iSIndex]);
      break;
    }
  }
}

Output matches

foreach($aMatch as $iIndex => $aRow)
{
  if($iIndex > 0) echo "\n";
  echo $aRow[0].','.$aRow[1];
}
?>

This example shows matching of Domain and Url, but not ID: (as your column A, column B example did)

foreach($aSecond as $iIndex => $sUrl)
{
  $sUrl = trim($sUrl);

  if(($iEq = strpos($sUrl, "=")) !== false)
  {
    $sUrlShort = substr($sUrl,0,$iEq);
  }
  else
    $sUrlShort = $sUrl;
 
  $aSecond[$iIndex] = array('u' => trim($sUrl), 's' => trim($sUrlShort));
}

foreach($aMaster as $sUrl)
{
  $sUrl = trim($sUrl);
  if(($iEq = strpos($sUrl, "=")) !== false)
  {
    $sUrlShort = substr($sUrl,0,$iEq);
  }
  else
    $sUrlShort = $sUrl;

  foreach($aSecond as $iSIndex =>  $aUrl)
  {
    if(strpos($sUrlShort, $aUrl['s']) !== false)
    {
      $aMatch[] = array($sUrl, $aUrl['u']);
      unset($aSecond[$iSIndex]);
      break;
    }
  }
}

foreach($aMatch as $iIndex => $aRow)
{
  if($iIndex > 0) echo "\n";
  echo $aRow[0].','.$aRow[1];
}


Regards

-r-
0
 

Author Comment

by:playstat
ID: 12443342
hi there this is the faster version

would it be possible to add yes or no to column a if match/nomatch and then url correction after the = in column E if no correction insert master url instead

<?php

$master_file = "list.txt";
$match_file = "match.txt";
$output_file = "file.csv";

$master_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($master_file)));
$match_array = preg_split('/[\\r\\n]+/', trim(file_get_contents($match_file)));
$second_array = array();

// apply trim() to remove blank chars
foreach ($master_array as $key=>$master_item) $master_array[$key] = trim($master_item);
foreach ($match_array as $key=>$match_item) $match_array[$key] = trim($match_item);

// analyse
foreach($master_array as $key=>$master_item) {
 $check = false;
 $master_parse = explode('=', $master_item);
 unset($second_item);
 foreach ($match_array as $match_item) {
  $match_parse = explode('=', $match_item);
  if ($master_parse[0] == $match_parse[0]) {
   $second_item = $match_item;
   $check = true;
   break;
  }
 }
 $second_array[$key] = isset($second_item) ? $second_item : $master_item;
 if ($check == false)
      $non_match[$key] = $master_item;
}

$output = array();
foreach($master_array as $key=>$master_item) {
 $output[] = "{$master_item},{$second_array[$key]},{$non_match[$key]},// call it nonmatch choice or something";
}

// leave non match as is but with an another coloumn at the end with nonmatch yes or no. If match insert yes if nomatch insert no. put in column D

$output = implode("\n",$output);

$handle = fopen($output_file,"wb") or die("Cannot open file");
fwrite($handle, $output) or die("Cannot write to file");
fclose($handle);

$non_match = implode("\n",$non_match);
$handle = fopen("nomatches.txt","w+") or die("Cannot open file");
fwrite($handle, $non_match) or die("Cannot write to file");
fclose($handle);

echo "Analysis Complete!";
?>
0
 

Author Comment

by:playstat
ID: 12443360
correction add choice yes or no match to column D in csv file

$output[] = "{$master_item},{$second_array[$key]},{$non_match[$key]},// call it nonmatch choice or something";
$output[] = A,B,C,D,corrected url from master if match file has same domain but different page ID";
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question