Forum Moderators: coopster

Message Too Old, No Replies

how to parse an array in a class?

         

timmy01

9:31 am on Nov 13, 2010 (gmt 0)

10+ Year Member




now i can parse 2 variables at a time, $a and $b to the class

but to do this if i want an multiple variables, like in an array...?


<?
class filter
{
function words( $var, $a, $b)
{
{
if( preg_match_all( "#$a(.*?)$b#s", $var, $match ) )
{
$this->line = $match;
}
}
}
}

$result = new filter;
$result->words($var, $a, $b );

?>

enigma1

11:03 am on Nov 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use the func_get_args to process multiple arguments inside a function.

function words()
$args_array = func_get_args();
$var = array_shift($args_array);
// $args_array now holds all other arguments a,b,c etc
..........

timmy01

11:27 am on Nov 13, 2010 (gmt 0)

10+ Year Member



the $var is actually a file.
I presume it should be read only once.

lets say:
$a='<b>';
$b='</b>';
$c='<u>';
$d='</u>';

or, if i put it in an array:
array("a" => <b>, </b> "b" => <u>, </u>);

how is that going to work?

cheerz

[edited by: eelixduppy at 4:15 pm (utc) on Nov 13, 2010]
[edit reason] disabled smileys [/edit]

enigma1

1:32 pm on Nov 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You call it like this:

$result->words($var, $a, $b, $c, $d );

and the function words is not dependent on the number of arguments because of the func_get_args.

Or you can pass the array in it and modify the words function to parse the second argument as the multiple values
$args_array = func_get_args();
if( count($args_array) != 2 ) {
echo '<br />Invalid Arguments Passed<br />';
return;
}
$var = array_shift($args_array);
$values_array = array_shift($args_array);
if( empty($values_array) || !is_array($values_array) ) {
echo '<br />Invalid Arguments Passed<br />';
return;
}
foreach($values array as $key => $value ) {
// process pair $key/$value
}

timmy01

3:44 pm on Nov 13, 2010 (gmt 0)

10+ Year Member



if i do it like this:

$result->words($var, $a, $b, $c, $d );

than how should that be parsed to the function?
i think i am not able to change it to

function words( $var, $a, $b, $c, $d)

the reason why is because of the next line:

if( preg_match_all( "#$a(.*?)$b#s", $var, $match ) )

where should i leave $c and $d here?

enigma1

4:04 pm on Nov 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know what you want to match/do with the preg_match_all function. You didn't specify. I mentioned how to pass a non-specific number of arguments to the words function. For example if you match variables in pairs like $a,$b and $c,$d you do 2 separate preg calls or but I don't know what you want to do.

timmy01

4:12 pm on Nov 13, 2010 (gmt 0)

10+ Year Member



what it needs to do is looking for tags in a file
like, tags between, for example, $a and $b

and between $c and $d