invalid argument to foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

invalid argument to foreach

Post by m3rajk »

i'm trying to build up a function that will return substring patterns (or a pattern once) and i don't understand why i get the following for output when i load the page:

your input was:



parsing found....
Warning: Invalid argument supplied for foreach() in /usr/users/m3rajk/WWW/JMT/randomTest/patternsplitbuild/longPREGway.php on line 17


after replacing...
Warning: Invalid argument supplied for foreach() in /usr/users/m3rajk/WWW/JMT/randomTest/patternsplitbuild/longPREGway.php on line 24




i'm pretty sure that i have it right though...

Code: Select all

<?php
$input=trim(htmlspecialchars(stripslashes(rawurldecode($_GET['input'])), ENT_NOQUOTES));
$parsefind=preg_match_all('|\[nocode]([^]])\[/nocode]|i', $input, $m, PREG_SET_ORDER);
$output=eregi_replace('\[nocode][^]]\[/nocode]', '&&&', $input);

?>
<html>
  <head>
    <title>perl build test page</title>
  </head>
  <body>
    <p>your input was:
      <br><?php echo $input; ?><br />
    </p>
    <p>parsing found....
<?php
foreach ($parsefind as $line){
  echo "      <br>$line";
}
?>
    </p>
    <p>after replacing...
<?php
foreach ($output as $line){
  echo "      <br>$line";
}
?>
    </p>
    <p>
      <form action="<?php echo $_SERVER[PHP_SELF]; ?>">
        <textarea name="input" cols="40" rows="10">
<?php
  if (isset($input)){
    echo $input;
  }else{
    echo 'enter input here';
  }
?></textarea>
        <br><input type="submit" value="parse!"><br />
      </form>
    </p>
  </body>
</html>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

http://people.brandeis.edu/~m3rajk/JMT/ ... REGway.php

can someone help me get that so i can input text into the text box and continue building to the function?

i do NOT want to be given the code that works. i wont learn a thing if i'm given that. i want to be given WHY it's going wrong so i know how to fix it. that way i WILL learn and wont have this issue in the future
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

after reading http://php.net/preg_match_all try

Code: Select all

<html>
	<body>
		<fieldset><legend>$_GET['input']</legend>
			<pre><?php echo isset($_GET['input']) ? $_GET['input'] : '&nbsp;'; ?></pre>
		</fieldset>
<?php
$input=trim(htmlspecialchars(stripslashes(rawurldecode($_GET['input'])), ENT_NOQUOTES));
?>
		<fieldset><legend>$input</legend>
			<pre><?php echo isset($input) ? $input : '&nbsp;'; ?></pre>
		</fieldset>
<?php		
$parsefind=preg_match_all('|\[nocode]([^]])\[/nocode]|i', $input, $m, PREG_SET_ORDER);
?>
		<fieldset><legend>$parsefind</legend>
			<pre><?php echo isset($parsefind) ? $parsefind : '&nbsp;'; ?></pre>
		</fieldset>
		<fieldset><legend>$m</legend>
			<pre><?php if (isset($m)) print_r($m); else echo '&nbsp;'; ?></pre>
		</fieldset>

<?php
$output=eregi_replace('\[nocode][^]]\[/nocode]', '&&&', $input);
?>
		<fieldset><legend>$output</legend>
			<pre><?php echo isset($output) ? $output : '&nbsp;'; ?></pre>
		</fieldset>

    <p>
      <form action="<?php echo $_SERVER[PHP_SELF]; ?>">
        <textarea name="input" cols="40" rows="10">
<?php
  if (isset($input)){
    echo $input;
  }else{
    echo 'enter input here';
  }
?></textarea>
        <br><input type="submit" value="parse!"><br />
      </form>
    </p>
  </body>
</html>
  • with
  • just a test
  • abc[nocode]1[/nocode]xyz
  • abc[nocode]123[/nocode]xyz
as input.
Post Reply