Page 1 of 2
how to return the value when the match is a subpattern?
Posted: Thu Oct 02, 2008 6:48 am
by swetha
Code: Select all
<html>
<head>
<title>preg_match_all</title>
</head>
<body>
<?php
function repeat2($reg,$search)
{
$result1=preg_match_all($reg,$search,$match);
echo "value of result1:" . $result1 . "<br/>";
if ($result1==0)
{
$output= "value not found";
}
else
{
$output="value found";
}
echo "repeat2:" .$output . "<br/>";
?>
<pre>
<?php
print_r($match);
?>
</pre>
<?php
//if there are no subpatterns
if (count($match)=='1')
{
return $match[0];
}
//if there are subpatterns
else
{
/* foreach($match as $disp)
{
echo "inside foreach:". $disp . "<br>";
$temp=$disp;
}*/
$temp=$match;
return $temp;
}
}
$assign=repeat2("/(how) are (you)/","how are you?what's up?");
foreach($assign as $disp)
{
echo "return from repeat2:".$disp . "<br/>";
}echo "no of matches:". count($assign)."<br/>";
?>
</body>
</html>
my main problem is to get the appropriate output when the match is a subpattern.the above coding displayed the output correctly when the match is a pattern
this is the output i get :
(
[0] => Array
(
[0] => how are you
)
[1] => Array
(
[0] => how
)
[2] => Array
(
[0] => you
)
)
return from repeat2:Array
return from repeat2:Array
return from repeat2:Array
no of matches:3
this is the output i should get
return from repeat2:how are you
return from repeat2:how
return from repeat2:you
no of matches:3
kindly note the commented foreach statement in the else portion.i added it to try it out,but it didnt display the appropriate match.
Re: need output for this
Posted: Thu Oct 02, 2008 6:50 am
by prometheuzz
What do you mean? You want someone else to execute this script and paste the output here in your thread? Why don't you do it yourself?
Re: how to return the value when the match is a subpattern?
Posted: Thu Oct 02, 2008 7:02 am
by swetha
nope....i mean am not getting the desired output.i have posted exactly what is the output i require,but with the above script am not getting the output.please read my posting once again to understand it clearly.
Re: how to return the value when the match is a subpattern?
Posted: Thu Oct 02, 2008 7:17 am
by prometheuzz
Edit: nvm
Re: how to return the value when the match is a subpattern?
Posted: Thu Oct 02, 2008 10:19 pm
by swetha
subpattern regular expression:
$assign=repeat2("/(how) are (you)/","how are you?what's up?");
For the above string ,im getting the following output:
return from repeat2:Array
return from repeat2:Array
return from repeat2:Array
no of matches:3
But i am supposed to get the following output:
this is the output i should get
return from repeat2:how are you
return from repeat2:how
return from repeat2:you
no of matches:3
the main problem is with return match[0]
when the pattern is not a subpattern , return match [0] works,but when the match is a subpattern we need return match[0],match[1]....etc.how do i do that?
Re: how to return the value when the match is a subpattern?
Posted: Fri Oct 03, 2008 3:00 am
by swetha
pls help me for this
Re: how to return the value when the match is a subpattern?
Posted: Fri Oct 03, 2008 3:21 am
by prometheuzz
Edit: nvm
Re: how to return the value when the match is a subpattern?
Posted: Fri Oct 03, 2008 3:24 am
by prometheuzz
Edit: nvm
Re: how to return the value when the match is a subpattern?
Posted: Fri Oct 03, 2008 4:14 am
by swetha
how do i pass the return value in the statement
return match[]?.kindly check the code which i have posted.
Re: how to return the value when the match is a subpattern?
Posted: Fri Oct 03, 2008 4:33 am
by prometheuzz
Edit: nvm
Re: how to return the value when the match is a subpattern?
Posted: Sat Oct 04, 2008 12:14 am
by swetha
This does not work for subpatterns
Re: how to return the value when the match is a subpattern?
Posted: Sat Oct 04, 2008 3:20 am
by prometheuzz
Edit: nvm
Re: how to return the value when the match is a subpattern?
Posted: Wed Oct 08, 2008 12:35 am
by swetha
what does this Edit:nvm mean?
Re: how to return the value when the match is a subpattern?
Posted: Wed Oct 08, 2008 1:01 am
by prometheuzz
swetha wrote:what does this Edit:nvm mean?
'nvm' means 'never mind'
Re: how to return the value when the match is a subpattern?
Posted: Thu Oct 09, 2008 11:14 pm
by swetha
pls help me with returning the value of the subpatterns too in the above code.