Jump to content

str_replace issue


LostDude25

Recommended Posts

I am trying to change the data in a .xml document. I am pulling contact infomation from a database and adding it to my phone system's speed dial list. I got everything else to work except this...

 

My code looks like:

 

$fileurl = 'data2.xml';
$rc = (starts with 300 and adds 1 with every loop)
$misc = (11 digit phone number with no spaces (ex 14025551212))
$fullname = (first and last name of customer)
$search = $rc."</c1> \n  <c2 /> \n  <c3 />";
$data = $rc."</c1> \n  <c2>9".$misc."</c2> \n  <c3>".$fullname."</c3> ";
$file_contents = file_get_contents($fileurl);
$fh = fopen($fileurl, "w");
$file_contents = str_replace($search,$data,$file_contents);
fwrite($fh, $file_contents);
fclose($fh);

 

The Data in the data2.xml file looks like this:

 

- <item row="100">
  <c0>100</c0> 
  <c1>*300</c1> 
  <c2 /> 
  <c3 /> 
  </item>
- <item row="101">
  <c0>101</c0> 
  <c1>*301</c1> 
  <c2 /> 
  <c3 /> 
  </item>
...ect with rows going up to 999

 

And I need it changed to look like this:

 

- <item row="100">
  <c0>100</c0> 
  <c1>*300</c1> 
  <c2>14025551212</c2>
  <c3>John Smith</c3>
  </item>
- <item row="101">
  <c0>101</c0> 
  <c1>*301</c1> 
  <c2>14025552121</c2>
  <c3>Jane Smith</c3>
  </item>
...ect with rows going up to 999

 

I am looking at replacing what I searched for ($search) and change it to the data I get from my database ($data). It seems like when I search for <c2 /> it errors like there is something stopping it. The problem is the format of the data.xml file, but I can not change it. If I leave off the <c2 /> and <c3 /> the code works fine but looks like this:

 

- <item row="100">
  <c0>100</c0> 
  <c1>*300</c1> 
  <c2>14025551212</c2>
  <c3>John Smith</c3>
  <c2 /> 
  <c3 /> 
  </item>

 

I got to get rid of the <c2 /> and <c3 /> and replace it with my data. Also eventually I will have less contacts and need to revert the data back to the original format. I have tried everything I know and no success. Anyone got any ideas that would work?  I am thinking its something simple that I am overlooking.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/246225-str_replace-issue/
Share on other sites

String based solution

 

<?php

$data[100] = array( 'name' => 'John Smith', 'number' => '14025551212' );
$data[101] = array( 'name' => 'Jane Smith', 'number' => '14025552121' );

$xml = <<<HEREDOC
- <item row="100">
  <c0>100</c0> 
  <c1>*300</c1> 
  <c2 /> 
  <c3 /> 
  </item>
- <item row="101">
  <c0>101</c0> 
  <c1>*301</c1> 
  <c2 /> 
  <c3 /> 
  </item>
...ect with rows going up to 999
HEREDOC;

foreach( $data as $key => $row ) {

if( ($pos = strpos( $xml, '<item row="'.$key.'">' )) === FALSE )
	continue;
$c2 = strpos( $xml, '<c2 />', $pos );
$c3 = strpos( $xml, '<c3 />', $pos );

$xml = substr( $xml, 0, $c2 ) . '<c2>'.$row['number'].'</c2>' .
	substr( $xml, $c2 + 6, $c3 - $c2 - 6 ) . 
	'<c3>'.$row['name'].'</c3>' . substr( $xml, $c3 + 6 );

}

echo '<pre>' . htmlspecialchars( $xml ) . '</pre>';

?>

Link to comment
https://forums.phpfreaks.com/topic/246225-str_replace-issue/#findComment-1264607
Share on other sites

I see what you are doing, but I think I am missing something. How do I incorporate that into the existing code. This is all in a loop that creates new information for every time, The John and Jane Smith was just an example.

 

I guess I am not understanding what to incorporate at the  <<<HEREDOC thing. I am thinking this is my .xml file but it errors every time I enter that.

 

Also with the code you provided, what variables go into where? I thought I provided enough information to fill this in.

Link to comment
https://forums.phpfreaks.com/topic/246225-str_replace-issue/#findComment-1265068
Share on other sites

It's a pretty straightforward example.

 

I'm not going to provide you with a plug and play solution, though with a decent amount of PHP knowledge it pretty much is.

 

I'll help you make it work with your existing code, but I won't do it for you. Read into the manual on anything you aren't sure of and give it an attempt. I'll help from there.

 

And I can't provide a complete solution without a complete data set. I can only assume with examples your have provided.

Link to comment
https://forums.phpfreaks.com/topic/246225-str_replace-issue/#findComment-1266075
Share on other sites

Sorry, I didn't as for you to create  a plug and play situation. I just wanted to know where in my script I needed to put it and how my variables would fit into your code. If that sounded like I wanted you to write my code, I apologize, that is not what I wanted.

 

I am going to include a copy of both files I am using for this. I sanitized them of any info that we don't want the whole world seeing. Just something about me, I write myself little notes in the script to explain what I am doing where I am doing it, so you will see a lot of //..... After I get the script done, I tidy it up so it isn't so long.

 

I had to upload the .xml file as a .txt file because of the rules of the forum, but it could easily be changed back to a .xml file when downloaded. It is saved on my computer as a .xml.

 

So if you could look at them and let me know if I have your code in the correct spot.

 

Thanks

 

phxml.php

<?php 
$rowcounter = 99;
$connectionstring = odbc_connect("database", "username", "password");  
//Query database for Names and numbers
$Query = "select pyemp#, pyfnam, pysnam
          from IESFILE.PYMAST
          where pystat = 'A'
          order by pyemp#"; 

$queryexe = odbc_do($connectionstring, $Query); 

while(odbc_fetch_row($queryexe)) 
    {
      $pyemp = trim(odbc_result($queryexe, 1));
      $pyfnam = trim(odbc_result($queryexe, 2));
      $pysnam = trim(odbc_result($queryexe, 3));

            
$Query1 = "select drmisc
           from IESFILE.DRIVERS
           where drcode = '$pyemp'"; 

$queryexe1 = odbc_do($connectionstring, $Query1); 

while(odbc_fetch_row($queryexe1)) 
{
    	$drmisc = trim(odbc_result($queryexe1,1));
    	$misc = preg_replace("/[^0-9]/","",$drmisc);
    	
//put in 'if' statement saying 'if $misc starts with 402 or 712, do nothing, if anything else, add a '1' except if it already starts with a '1'

      if (preg_match('/^1/', $misc)){
}elseif (preg_match('/^402/', $misc)){
}elseif (preg_match('/^712/', $misc)){
}else{
$misc = "1".$misc;	
}	
}

$rowcounter = $rowcounter + 1; 
$rc = $rowcounter + 200;
$fullname = $pyfnam." ".$pysnam;

//File to be seached and edited...

$fileurl = 'data2.xml';

//Search file for this line of code...

//$search = "<item row=\"".$rowcounter."\">\n\t<c0>".$rowcounter."</c0>\n<c1>*".$rc."</c1>\n<c2 />\n<c3 />"; //Long version...Too Much Info
//$search = $rc."</c1>";    //This one works, but doesn't get rid of the <c2 /> or <c3 />
$search = $rc."</c1> \n  <c2 /> \n  <c3 />";

//Create Data line to be entered into file...

//$data = "<item row=\"".$rowcounter."\">\n\t<c0>".$rowcounter."</c0>\n\t<c1>*".$rc."</c1>\n\t<c2>9".$misc."</c2>\n\t<c3>".$fullname."</c3>"; //too much info
$data = $rc."</c1> \n  <c2>9".$misc."</c2> \n  <c3>".$fullname."</c3> ";

//This opens file and searches the file and changes the searched info to the data created...

$file_contents = file_get_contents($fileurl);
$fh = fopen($fileurl, "w");
$file_contents = str_replace($search,$data,$file_contents);
fwrite($fh, $file_contents);

//added xyph code

$data2 = array( 'name' => $fullname, 'number' => $misc );

//$xml is data2.xml

$xml = <<<HEREDOC
$file_contents
HEREDOC;

foreach( $data2 as $key => $row ) {

if( ($pos = strpos( $xml, '<item row="'.$row.'">' )) === FALSE )
	continue;
$c2 = strpos( $xml, '<c2 />', $pos );
$c3 = strpos( $xml, '<c3 />', $pos );

$xml = substr( $xml, 0, $c2 ) . '<c2>'.$row['number'].'</c2>' .substr( $xml, $c2 + 6, $c3 - $c2 - 6 ) . '<c3>'.$row['name'].'</c3>' . substr( $xml, $c3 + 6 );

}

    unset($misc); //unset variable so it doesn't use the previous employee's number again...
    $misc = " no number";  // Any number will do here, ***will have to go into the database and add everyone phone number.

fclose($fh);

}   

echo '<pre>' . htmlspecialchars( $xml ) . '</pre>';
odbc_close($connectionstring); 
echo "done!";

//Things that need to happen to make this work. 
//Run when anyone is hired or fired/quits
//Number for the person is put in the Driver Master's Misc Line, if local, just 402, if long distance in NE with 402, add the 1.
//if driver's # is local, just put in 402 EXAMPLE  Omaha 402-733-2200 DONE!
//if driver's # is long distance in the 402 area code, add "1" EXAMPLE  Lincoln 1-402-733-2200 DONE!
//This applies to 712 also DONE!
//if driver lives anywhere else, no need to add the '1', program will add it for you. DONE!
//take info and place it into xml file
//make if statement to update file after xml is already used and needs to be updated
?> 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/246225-str_replace-issue/#findComment-1266545
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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