Forum Moderators: coopster
Guild <?php echo $_GET["name"];?>.<br />
<?php
$character = $_GET["name"];
$url = 'http://www.example.com?subtopic=guilds&page=view&GuildName=';
$url2 = ($url.$character);
$page = file_get_contents($url2);
$pattern = '/&name=(.+)"/';
preg_match_all($pattern, $page, $matches, PREG_PATTERN_ORDER );
$url = 'http://www.example.com?subtopic=characters&name=';?>
This obviously constructs a URL from 2 variables from a form, and creates a string variable of the URL.
I then filter through the variable $page to find the data I am looking for (Names of all characters in a guild)
My problem is from the source text, I get some values in my array that I don't want and some that I do..
The variables in $matches[0][[0] to $matches[0][[17] i do not want and the variables $matches[0][[0] to $matches[1][[17] i do want, from the array.
When I use:
foreach ($matches as $val) {
echo "Character: " . $val[0] . "\n";
}
It starts with all the variables I do not want to use. Therefore I can't work out how I can loop specifically the variables I do need.
Hope someone can help,
thanks alot
The number of variables dynamic (i don't know how many will be in the array)
Could I do this?
foreach ($matches as $val) {
if ($matches[1][[0] == $matches[1][
thanks
if ($matches[1][[0] == $matches[1][[17]) {
$last_match = count($matches[1])-1; // as arrays start from 0
if ($matches[1][0] == $matches[1][$last_match]) {
//
}
For example:
$page = file_get_contents($url2);
$pattern = '/>(.[a-z]+)<\/A>/';
preg_match_all($pattern, $page, $matches, PREG_SET_ORDER );
Gets the array of variables I want, which works fine.
Then I try the following:
$page2 = file_get_contents($url3);
$pattern2 = '/$bal[1]/';
foreach ($matches[14][0] as $val) {
preg_match_all($matches, $page2, $matches2, PREG_SET_ORDER );
print_r ($matches2);
}
I get an error saying that I can't use arrays and must use strings in my preg_match_all function.
So, I don't know how to go through each variable in my first array, and pregmatch them to anything on the next page. (the number of variables in my first array is not constant and also, the only variables I want in my first array are:
$matches[1][0], $matches[2][0], $matches[3][0], $matches[4][0] etc..
Any help is appreciated, this is driving me crazy! thanks
replace this:
foreach ($matches[14][0] as $val) {
preg_match_all($matches, $page2, $matches2, PREG_SET_ORDER );
print_r ($matches2);
}
with:
foreach ($matches[14][0] as $val) {
preg_match_all($val, $page2, $matches2, PREG_SET_ORDER );
print_r ($matches2);
}
I can collect 2 arrays of Data. The first away, holds the data (collection of names) i need in $matches[X][1] (where X is any number between 0 and the last element).
The second array holds many names, some of which are also in array 1.
I am trying desperately to find a way to loop, for every name in array 1, check to see if this name is in array 2.
I have had so much trouble with foreach, I have just tried to do so with an If statement for now..
I have:
$t = 1;
if ($matches[3][1] == $matches2[$t][1])
{
$t++;
echo "online";
}
I couldn't get this to work but hopefully it demonstrates the princple of what i am trying to do, to someone who knows what to do!
cheers
I am trying desperately to find a way to loop, for every name in array 1, check to see if this name is in array 2.
Seems you can do it with array_intersect [php.net]
$result = array_intersect($array1, $array2)
print_r(array_intersect($matches, $online));
This is everything I have written:
Guild <?php echo $_GET["name"];?>.<br />
<?php
$guild = $_GET["name"];
$guild = preg_replace('/(\ )/','+',$guild);
$url = 'http://www.example.com';
$url2 = ($url.$guild);
$page = file_get_contents($url2);
$pattern = '/&name=([^0-9]+)"/';
preg_match_all($pattern, $page, $matches, PREG_SET_ORDER );
print_r ($matches);
foreach ($matches as $bal) {
echo "<table border=\"1\">";
echo "<tr><th>$bal[1]</th>";
}
$url3 = 'http://www.example2.com';
$page2 = file_get_contents($url3);
$pattern2 = '/(&name=)([^0-9]+)"/';
preg_match_all($pattern2, $page2, $online, PREG_SET_ORDER );
//print_r ($online);
print_r(array_intersect($matches, $online));
I can't see what is wrong here, that the 2 array's wont intersect. I checked using var_dump() that each array was in strings.
$names = array('george', 'fred', 'bill', 'harry', 'ron');
$others = array('harry', 'ron', 'ginny');
$matching = array();
foreach ($names as $name) {
foreach ($others as $other) {
if ($name = $other) {
$matching[] = $name;
}
}
}