Page 1 of 2
edit an array
Posted: Wed Jan 05, 2005 8:43 pm
by videogame
hi everyone,
i have an array called $out containing many strings. I need a function that will create an array called $out2 in which each key has "parse.php?website=" added to the beginning.
ex: original key is "hello"
after function is "parse.php?website=hello"
how can this be achieved?
thanks
Posted: Wed Jan 05, 2005 8:50 pm
by John Cartwright
Code: Select all
<?php
$out = array('hello','goodbye','blah');
foreach ($out as $value)
{
echo '<a href="parse.php?website='.$value.'">'.$value.'</a><br />';
}
?>
outputs
Code: Select all
<a href="parse.php?website=hello">hello</a>
<a href="parse.php?website=goodbye">goodbye</a>
<a href="parse.php?website=blah">blah</a>
but what I think you want is
Code: Select all
<?php
$out = array('hello','goodbye','blah');
$out2 = array();
foreach ($out as $value)
{
$out2[] = 'parse.php?website='.$value;
}
?>
Posted: Wed Jan 05, 2005 8:52 pm
by rehfeld
Code: Select all
$strings = array(
'foo',
'bar',
'baz';
);
$new_strings = array();
foreach ($strings as $string) {
$new_strings[] = 'parse.php?website='.$string;
}
Posted: Wed Jan 05, 2005 9:05 pm
by feyd
videogame, you said key, but do you really mean value? Because these two replies use the value, not the key.
Posted: Wed Jan 05, 2005 9:10 pm
by videogame
Phenom wrote:Code: Select all
<?php
$out = array('hello','goodbye','blah');
foreach ($out as $value)
{
echo '<a href="parse.php?website='.$value.'">'.$value.'</a><br />';
}
?>
outputs
Code: Select all
<a href="parse.php?website=hello">hello</a>
<a href="parse.php?website=goodbye">goodbye</a>
<a href="parse.php?website=blah">blah</a>
but what I think you want is
Code: Select all
<?php
$out = array('hello','goodbye','blah');
$out2 = array();
foreach ($out as $value)
{
$out2[] = 'parse.php?website='.$value;
}
?>
this is my array:
Array
(
[0] => /imghp?hl=en&tab=wi&ie=UTF-8
[1] => /grphp?hl=en&tab=wg&ie=UTF-8
[2] => /nwshp?hl=en&tab=wn&ie=UTF-8
[3] => /options/index.html
[4] => /advanced_search?hl=en
[5] => /preferences?hl=en
[6] => /language_tools?hl=en
[7] =>
http://www.google.com/tsunami_relief.html
[8] =>
http://www.google.ca/fr
[9] => /ads/
[10] => /services
[11] => /intl/en/about.html
[12] =>
http://www.google.com/ncr
)
for some reason the script gives me only
Array
(
http://www.google.com/ncr
)
i need it to edit the array and add the text to each value
Posted: Wed Jan 05, 2005 9:11 pm
by videogame
feyd wrote:videogame, you said key, but do you really mean value? Because these two replies use the value, not the key.
.
yes i believe it should be value
Posted: Wed Jan 05, 2005 9:12 pm
by videogame
basically i need it to take:
Code: Select all
Array
(
[0] => /imghp?hl=en&tab=wi&ie=UTF-8
[1] => /grphp?hl=en&tab=wg&ie=UTF-8
[2] => /nwshp?hl=en&tab=wn&ie=UTF-8
[3] => /options/index.html
[4] => /advanced_search?hl=en
[5] => /preferences?hl=en
[6] => /language_tools?hl=en
[7] => http://www.google.com/tsunami_relief.html
[8] => http://www.google.ca/fr
[9] => /ads/
[10] => /services
[11] => /intl/en/about.html
[12] => http://www.google.com/ncr
)
and change it to:
Code: Select all
Array
(
[0] => parse.php?website=/imghp?hl=en&tab=wi&ie=UTF-8
[1] => parse.php?website=/grphp?hl=en&tab=wg&ie=UTF-8
[2] => parse.php?website=/nwshp?hl=en&tab=wn&ie=UTF-8
[3] => parse.php?website=/options/index.html
[4] => parse.php?website=/advanced_search?hl=en
[5] => parse.php?website=/preferences?hl=en
[6] => parse.php?website=/language_tools?hl=en
[7] => parse.php?website=http://www.google.com/tsunami_relief.html
[8] => parse.php?website=http://www.google.ca/fr
[9] => parse.php?website=/ads/
[10] => parse.php?website=/services
[11] => parse.php?website=/intl/en/about.html
[12] => parse.php?website=http://www.google.com/ncr
)
then this array should be saved as one variable called $out2
Posted: Wed Jan 05, 2005 9:23 pm
by John Cartwright
Code: Select all
<?php
$out = array('hello','goodbye','blah');
$out2 = array();
foreach ($out as $value)
{
$out2[] = 'parse.php?website='.$value;
}
print_r($out2);
?>
outputs
Code: Select all
Array ( ї0] => 'parse.php?website=hello' ї1] => 'parse.php?website=goodbye' ї2] => 'parse.php?website=blah' )
Posted: Wed Jan 05, 2005 9:26 pm
by feyd
videogame, please read the link in my signature.
Posted: Wed Jan 05, 2005 9:27 pm
by videogame
ok... thank you very much. the script works wonderfully.
i need one more thing.
this is my script
Code: Select all
$out2 = array();
foreach ($out['href'] as $value)
{
//$out2['href'] = 'parse.php?website='.$value;
$tempv = 'parse.php?website='.$value;
$out2[] = $tempv;
}
some array entires start with
http://www. and others do not. How do i create an IF statement to check for this before continuing to the next step?
im thinking of something like this:
Code: Select all
$out2 = array();
foreach ($out['href'] as $value)
{
//$out2['href'] = 'parse.php?website='.$value;
//check if $value starts with 'http://www.'
//do this if $value starts with 'http://www.'
$tempv = 'parse.php?website='.$value;
//do this if $value does not start with 'http://www.'
$tempv = 'parse.php?website='$url & $value;
$out2[] = $tempv;
}
Posted: Wed Jan 05, 2005 9:39 pm
by feyd
Code: Select all
$tempv = 'parse.php?website=';
if(preg_match('#^http://www\.#i', $value))
$tempv .= $value;
else
$tempv .= 'http://www.' . $value;
$out2ї] = $tempv;
Posted: Wed Jan 05, 2005 9:58 pm
by videogame
yup, amazing works. works perfectly
Posted: Thu Jan 06, 2005 8:55 pm
by videogame
i have one more question. in the IF statement, can it check if the array value ends in '.html' or '.htm' or '.asp' or '.php'?
Posted: Thu Jan 06, 2005 9:03 pm
by feyd
yes...
Code: Select all
$tempv = 'parse.php?website=';
if(preg_match('#^http://www\.#i', $value))
$tempv .= $value;
else
$tempv .= 'http://www.' . $value;
if(preg_match('@[^#\?].*?\.(html?|asp|php)$@i', $value))
{
// do whatever for found
}
else
{
// do whatever for _not_ found
}
$out2[] = $tempv;
Posted: Fri Jan 07, 2005 3:25 am
by onion2k
preg_match when a strpos would work instead.. tsk..
