Jump to content

Make array and loop it - need help


liquidmind

Recommended Posts

preg_match.jpg

 

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.

Link to comment
Share on other sites

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>";

}

 

}

?>

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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>";

}

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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++;
}
}
?>

Link to comment
Share on other sites

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++;

}

}

Link to comment
Share on other sites

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."";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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