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.