liquidmind Posted April 2, 2008 Share Posted April 2, 2008 The input list needs to be exploded based on new line, and then a preg_match performed using each list item. Loop till last item, then output results. Mostly have trouble how to set up the array, the foreach, and the count to increment. I have the echo down pat. Quote Link to comment Share on other sites More sharing options...
zenag Posted April 2, 2008 Share Posted April 2, 2008 have a look at this...... <?php $name=array("abc","def","efg"); foreach($name as $value) { if (preg_match('/^def/',$value )) { echo $value."A match was found.</br>"; } else { echo $value."A match was not found.</br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
liquidmind Posted April 2, 2008 Author Share Posted April 2, 2008 The list items aren't the same everytime so I can't put array("abc","def","efg"); into the code. The array needs to be taken from $_POST. I have this so far: <? if (!isset($_POST['itemlist'])) { ?> <form name="form" method="post"> <textarea rows=5 cols=20 name="itemlist" value="<?=$_POST['itemlist']?>" </textarea> <input type="submit" name="formsubmit" value="Submit!" class="button_style"> </form> <? } else { $itemlist = explode("\n",$_POST['itemlist']); $count = 0; foreach ($itemlist AS $item) { Quote Link to comment Share on other sites More sharing options...
zenag Posted April 2, 2008 Share Posted April 2, 2008 its works pretty as well for me... else { $itemlist = explode("\n",$_POST['itemlist']); $count = 0; foreach ($itemlist AS $item) { echo $item; if (preg_match('/eggs/',$item )) { echo $value."A match was found.</br>"; } else { echo $value."A match was not found.</br>"; } Quote Link to comment Share on other sites More sharing options...
liquidmind Posted April 2, 2008 Author Share Posted April 2, 2008 So I don't need to do $var = array() anywhere ? Right now what it is doing is giving me the preg_match results for the last item in the list only. If I have one item in the list it will give me the results for that one item. If the list is two items then it will give me the results for item 2 while showing null results for 1. If the list is three items, it will give me the results for item 3 while showing null results for 1 and 2...etc. Quote Link to comment Share on other sites More sharing options...
zenag Posted April 2, 2008 Share Posted April 2, 2008 can u show the code pls.. Quote Link to comment Share on other sites More sharing options...
liquidmind Posted April 2, 2008 Author Share Posted April 2, 2008 Where it says "xxxx" it's a filler to protect the innocent. <? if (!isset($_POST['itemlist'])) { ?> <form name="form" method="post"> <textarea rows=5 cols=20 name="itemlist" value="<?=$_POST['itemlist']?>" </textarea> <input type="submit" name="formsubmit" value="Submit!" class="button_style"><br /> </form> <? } else { $itemlist = explode("\n",$_POST['itemlist']); $count = 0; foreach ($itemlist AS $item) { $ab=curl_init(); $link="xxxx" curl_setopt($ab,CURLOPT_URL,$link); curl_setopt($ab, CURLOPT_HEADER, 0); ob_start(); curl_exec($ab); curl_close($ab); $item_results=ob_get_contents(); ob_end_clean(); preg_match (xxxx) $count_matches=$match[1]; if($count_matches=="") $count_match=0; echo "<br/>".$item." ".$count_match.""; $count++; } } ?> Quote Link to comment Share on other sites More sharing options...
zenag Posted April 2, 2008 Share Posted April 2, 2008 try this........ $link="/xxxx/"; if(preg_match ($link,$item)) { $count_matches=$match[1]; if($count_matches=="") $count_match=0; echo "found".$item." ".$count_match."<br/> "; }else {echo "notfound".$item." ".$count_match."<br/> "; } $count++; } } Quote Link to comment Share on other sites More sharing options...
liquidmind Posted April 2, 2008 Author Share Posted April 2, 2008 count_matches is the found or not found. If it returns matches then found, if not, then not found. No need to add echo "found" or "not found". The count_matches=0 is the "not found". With that in mind there is no point to doing an if statement at preg_match. Right now what it is doing is giving me the correct preg_matches results for the last item in the list only. If I have one item in the list it will give me the results for that one item. If the list is two items then it will give me the results for item 2 while showing null results for 1. If the list is three items, it will give me the results for item 3 while showing null results for 1 and 2...etc. echo "<br/>".$item." ".$count_match.""; Should be echo "<br/>".$item." ".$count_matches.""; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.