Changing my script to work sequentially

Hi

I am using this function which spins text.

I am trying to change this so that it goes sequentially through
all of the available elements in the array and then when finished
starts again with the first element of the array.

I have tried a couple of things …

It is partially working but failing …

Here is the code I have …


function inStr($needle, $haystack){
    return @strpos($haystack, $needle) !== false;
    }

function str_replaceFirst($s,$r,$str){
    $l = strlen($str);
    $a = strpos($str,$s);
    $b = $a + strlen($s);
    $temp = substr($str,0,$a) . $r . substr($str,$b,($l-$b));
    return $temp;
    }

function sqn_spin($pass){
   $mytext = $pass;
   $idx = 0;
   while(inStr("}",$mytext)){
       $rbracket = strpos($mytext,"}",0);
       $tString = substr($mytext,0,$rbracket);
       $tStringToken = explode("{",$tString);
       $tStringCount = count($tStringToken) - 1;
       $tString = $tStringToken[$tStringCount];
       $tStringToken = explode("|",$tString);
       $tStringCount = count($tStringToken) - 1;
       //$i = rand(0,$tStringCount);
       $replace = $tStringToken[$idx++];
       $tString = "{".$tString."}";
       $mytext = str_replaceFirst($tString,$replace,$mytext);
   }
   return $mytext;
}

// I use the funtions to generate 100 new files which should
// sequentially step through the alternative words.

$Db_arts = 100;

for ($art = 0; $art <= $Db_arts; $art++) {

    // Create files that are named: 1.txt, 2.txt etc.

   $filename = "{$art}.txt";
   $filename = $path.$filename;

   $Db_text = str_replace("'","`",$Db_text);
   if($Db_rand_cd == 'y') {
    $spun_text = rand_spin($Db_text);
    }
   else {
    $spun_text = sqn_spin($Db_text);
            }
    $wordCount = explode(" ",$spun_text);
    $wordCount = count($wordCount);
    $spun_text = str_replace(chr(13).chr(10),"<br />".chr(13).chr(10),$spun_text);

    $spun_text = wordwrap($spun_text, 70, "\
");
    file_put_contents($filename,$spun_text );

    echo "<br>Created File: $filename, Word Count: $wordCount";

    } // end different articles

But …
I am getting these errors:

Notice: Undefined offset: 4 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 6 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 7 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 8 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 9 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 10 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 11 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 12 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 14 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 15 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 16 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 17 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 18 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 19 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 20 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 21 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 22 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 23 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 24 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 25 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 26 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 27 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 28 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 29 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 30 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 31 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 32 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 33 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 34 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 35 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 36 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 37 in /home/com567b/public_html/auto_spin.php on line 59
Notice: Undefined offset: 38 in /home/com567b/public_html/auto_spin.php on line 59

Created File: /home/com567b/spun/0.txt, Word Count: 52

Then I get the same errors for 1.txt
and so on for 100 files.

I don’t know why it always starts at offset 4, then misses offset 5, then continues up to
offset 38.

Must be a reason ??

line 59 is : $replace = $tStringToken[$idx++];

I think the problem is … I am not checking to see if we have
run out of the offsets, before incrementing $idx
… but I may be wrong

Is $tStringCount the number of offsets of $tStringToken ??

How can I add a counting check ?

Any help very much appreciated …

.

Try adding the following lines to the top of your script:


 
# John_Betong
   error_reporting(-1); 
   ini_set('display_errors',1);

   $path             = 'd:/xampp/htdocs/kill/';
   $Db_text        = 'this-is-a-test-to-see-if-it-works';
   $Db_rand_cd = 'y';
   $Db_arts        = 10; 

# added missing function
function rand_spin($result='')
{
	return $result;
}


Outpit:


Created File: d:/xampp/htdocs/kill/0.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/1.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/2.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/3.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/4.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/5.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/6.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/7.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/8.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/9.txt, Word Count: 1
Created File: d:/xampp/htdocs/kill/10.txt, Word Count: 1

Hi John,

The function rand_spin() is in my script,
I just didn’t show it to avoid taking attention to a function which works.

I also have input via form …

The complete script is …


if(session_id() == ''){session_start();}

require_once("my_functions.php");

if (@$_SESSION['auth'] != "yes" ){
	 header("Location: index.php");
	 exit();
	}  // end if

set_time_limit(10);
function inStr($needle, $haystack){
    return @strpos($haystack, $needle) !== false;
	}

function str_replaceFirst($s,$r,$str){
    $l = strlen($str);
    $a = strpos($str,$s);
    $b = $a + strlen($s);
    $temp = substr($str,0,$a) . $r . substr($str,$b,($l-$b));
    return $temp;
	}

function rand_spin($pass){
    $mytext = $pass;
    while(inStr("}",$mytext)){
        $rbracket = strpos($mytext,"}",0);
        $tString = substr($mytext,0,$rbracket);
        $tStringToken = explode("{",$tString);
        $tStringCount = count($tStringToken) - 1;
        $tString = $tStringToken[$tStringCount];
        $tStringToken = explode("|",$tString);
        $tStringCount = count($tStringToken) - 1;
        $i = rand(0,$tStringCount);
        $replace = $tStringToken[$i];
        $tString = "{".$tString."}";
        $mytext = str_replaceFirst($tString,$replace,$mytext);
    }
    return $mytext;
	}

function sqn_spin($pass){
   $mytext = $pass;
   $idx = 0;
   while(inStr("}",$mytext)){
       $rbracket = strpos($mytext,"}",0);
       $tString = substr($mytext,0,$rbracket);
       $tStringToken = explode("{",$tString);
       $tStringCount = count($tStringToken) - 1;
       $tString = $tStringToken[$tStringCount];
       $tStringToken = explode("|",$tString);
       $tStringCount = count($tStringToken) - 1;
       //$i = rand(0,$tStringCount);
       $replace = $tStringToken[$idx++];
       $tString = "{".$tString."}";
       $mytext = str_replaceFirst($tString,$replace,$mytext);
   }
   return $mytext;
}	

IF( isset($_POST['ASPin3']) && $_POST['ASPin3'] == "SPn365"){
	
	$N_text = $_POST['x_text'];
	$N_arts = $_POST['x_arts'];
	$N_rand_cd = $_POST['x_rand_cd'];
		
	$Db_text = safe_sql($N_text);	
	$Db_arts = safe_sql($N_arts);	
	$Db_rand_cd = safe_sql($N_rand_cd);	
		
	$path = "/home/com567b/spun/";
	
	for ($art = 0; $art <= $Db_arts; $art++) {
	
		// Create files that are named: 1.txt, 2.txt etc.
		
		$filename = "{$art}.txt";
		$filename = $path.$filename; 

		$Db_text = str_replace("'","`",$Db_text);
		if($Db_rand_cd == 'R') {
			$spun_text = rand_spin($Db_text);
			}
		else {
			$spun_text = sqn_spin($Db_text);
			}
			
		$wordCount = explode(" ",$spun_text);
		$wordCount = count($wordCount);
		$spun_text = str_replace(chr(13).chr(10),"<br />".chr(13).chr(10),$spun_text);
		
		$spun_text = wordwrap($spun_text, 70, "\
"); 	
		file_put_contents($filename,$spun_text );
	 
		echo "<br>Created File: $filename, Word Count: $wordCount";	
						
		} // end different articles
	  echo "<br><br><b>Total $art articles created</b><br>";	
	}  // END IF - END OF PROCESS FORM
	
$N_arts = 100;
$N_text = "Spin Text Article";
$N_rand_cd = "S";
require_once ("auto_spin_fm.php");

the my_functions.php contains:

//  Turn ON the error display
ini_set ("display_errors", "1");
error_reporting(E_ALL);   

( I don’t have : error_reporting(-1); though - should I add that ? )

If I set $Db_rand_cd = ‘R’
then it works OK … that’s because it uses the random function

But it is the sequential function - sqn_spin() that I can not make work.

Any thoughts ?

Thanks.

.

Hi jekko,

>>> ( I don’t have : error_reporting(-1); though - should I add that ? )
The -1 parameter is the most strict error_reporting() value and recommended for debugging.

It is difficult to test your script without relevant data so may I suggest including the following function at the beginning of your script.



/*
  Debug finction:
  usage:
      xx('value to be tested');
*/
function xx($var='')
{
  echo '<pre>';
    print_r($var);
  echo '</pre>';
  die;
}


To debug your script:



Repeat:
   1. find the point where the script is causing problems
   2. insert xx( $valueToBeTested );
   3. The debug function will be called
       show the formatted $valueToBeTested
        then stop your script.

    If the $valueToBeTested is not what is expected
       step back to previous point in your code.

    else
       move the debug function to the next step in your script.

Until your problem is solved.

Have fun debugging