edit an array
Moderator: General Moderators
edit an array
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
<?php
$out = array('hello','goodbye','blah');
foreach ($out as $value)
{
echo '<a href="parse.php?website='.$value.'">'.$value.'</a><br />';
}
?>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>Code: Select all
<?php
$out = array('hello','goodbye','blah');
$out2 = array();
foreach ($out as $value)
{
$out2[] = 'parse.php?website='.$value;
}
?>
Last edited by John Cartwright on Wed Jan 05, 2005 8:54 pm, edited 3 times in total.
Code: Select all
$strings = array(
'foo',
'bar',
'baz';
);
$new_strings = array();
foreach ($strings as $string) {
$new_strings[] = 'parse.php?website='.$string;
}this is my array:Phenom wrote:outputsCode: Select all
<?php $out = array('hello','goodbye','blah'); foreach ($out as $value) { echo '<a href="parse.php?website='.$value.'">'.$value.'</a><br />'; } ?>
but what I think you want isCode: 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>
Code: Select all
<?php $out = array('hello','goodbye','blah'); $out2 = array(); foreach ($out as $value) { $out2[] = 'parse.php?website='.$value; } ?>
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
basically i need it to take:
and change it to:
then this array should be saved as one variable called $out2
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
)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
)Code: Select all
<?php
?>
Last edited by videogame on Wed Jan 05, 2005 9:29 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
<?php
$out = array('hello','goodbye','blah');
$out2 = array();
foreach ($out as $value)
{
$out2[] = 'parse.php?website='.$value;
}
print_r($out2);
?>Code: Select all
Array ( ї0] => 'parse.php?website=hello' ї1] => 'parse.php?website=goodbye' ї2] => 'parse.php?website=blah' )ok... thank you very much. the script works wonderfully.
i need one more thing.
this is my script
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:
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;
}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;
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$tempv = 'parse.php?website=';
if(preg_match('#^http://www\.#i', $value))
$tempv .= $value;
else
$tempv .= 'http://www.' . $value;
$out2ї] = $tempv;- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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;