Copy link to clipboard
Copied
Is there a way to write this where it will limit the number of times that it runs - say 20 times?
while($p_link = array_search("0", $links))
{
$links[$p_link] = "1";
// do this
}
It uses the ternary operator.
Copy link to clipboard
Copied
Coming from a non php coder...most languages allow you to include more than one condition in a while loop. So initialize a counter variable that you increment within the loop. And (not sure of the AND operator in php) it with the other condition. Something like:
$counter=0
while($p_link = array_search("0", $links) && $counter <20)
{
$links[$p_link] = "1";
$counter++
// do this
}
Copy link to clipboard
Copied
We'll make a PHP coder of you yet. ![]()
The only thing that's wrong is the semicolon missing from the end of each statement:
$counter=0;
while($p_link = array_search("0", $links) && $counter <20)
{ $links[$p_link] = "1";
$counter++;
// do this
}
Copy link to clipboard
Copied
Thank you, I just could not figure this out as I wanted to add an entirely new while loop. However, this still is not giving me the results I want.
What I have is code that will get all the internal links from a url, parse through those links for certain content (set by the preg_match_all($search, $contents, $search_results)), then add that content to an array ($gold).
However, I want to limit the number of links that it parse through to say 20 links.
Using the information your provider earlier ($counter) – it skips the parsing through the links and just seems to skip over the get content and following code.
I have tried putting the counter in other places or even using additional while loops but have gotten nowhere. Can you tell me where I should be trying to limit the number of links to parse?
Code is attached
Copy link to clipboard
Copied
I don't have time to go through nearly 300 lines of code. However, since you want to limit $gold to 20 items, it seems as though you could alter the following section of code:
$num_results = count($search_results[2]);
// split each matched phrase into words and add to the array - David Powers
for ($i = 0; $i < $num_results; $i++)
{
$gold = array_merge($gold, explode(' ', $search_results[2][$i]));
}
Change it to this:
$num_results = count($search_results[2]);
// limit the number of results to a maximum of 20
$num_results = ($num_results > 20) ? 20 : $num_results;
// split each matched phrase into words and add to the array - David Powers
for ($i = 0; $i < $num_results; $i++)
{
$gold = array_merge($gold, explode(' ', $search_results[2][$i]));
}
Copy link to clipboard
Copied
If you do get some time, can you explain this:
num_results = ($num_results > 20) ? 20 : $num_results;
Not sure I understand the ? 20 : $num_results; part.
Thanks
Copy link to clipboard
Copied
It uses the ternary operator.
Copy link to clipboard
Copied
Thanks - that makes sense. It is still causing me problems. But, I am trying something else - we will see if this works.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more