how to return 2 values from a function in this case:

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

how to return 2 values from a function in this case:

Post by swetha »

ive written the following function for preg_match_all

Code: Select all

 
function repeat($reg,$search)
{
   $result1=preg_match_all($reg,$search,$match);
   if ($result1==0) 
    {
    $output= "value not found";
     }
  else
  {
      //echo "preg_match:result not found";
       $output="value found";
  }
 return $match[0];
}
 
inside a switch statement i assign

Code: Select all

 
$constr =repeat($reg,$search); 
 

inside a textarea i have the following code

Code: Select all

 
<textarea  name="resulttxt" readonly="readonly">
<?php if (isset($_POST['submit']))
{ 
  $i=1;
  foreach($constr as $value)
  {
      echo "Matched Value " .$i. ":" .$value;
      echo "\n";
      $i++;
  } 
}
else{echo " ";}?></textarea> 
 
my doubt is how do i return the $result1 also along with $match[0] and print its value in the textarea.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: how to return 2 values from a function in this case:

Post by VladSun »

Return an array fillded up with what you need.
There are 10 types of people in this world, those who understand binary and those who don't
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return 2 values from a function in this case:

Post by swetha »

but in this case how do i return it and assign the value?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: how to return 2 values from a function in this case:

Post by Luke »

Are you trying to do something like this?

Code: Select all

function twoValues() {
    return array('value1', 'value2');
}
 
list($value1, $value2) = twoValues();
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return 2 values from a function in this case:

Post by swetha »

Im having following 2 functions repeat1 and repeat2.
In the first case,i have an array which is suppose to return 2 values.one is the number of matched values and the other one is the contents of match[0].But im not getting the reuqire output.I have displayed the ouput that i am getting.The desired output is displayed in the end.Kindly help

Code: Select all

 
<?php
echo "function repeat1" . "<br/>";
function repeat1($reg,$search)
{
    $result1=preg_match_all($reg,$search,$match);
    if ($result1==0) 
    {
          $output= "value not found";
    }
    else
    {
         //echo "preg_match:result not found";
          $output="value found";
    }
    echo "repeat1:" .$output;
    //return $match[0];
    $retarray=array($result1,$match[0]);  
    return $retarray;
}   
$assign=repeat1("/[a-z]/","asfaqwqeq"); 
echo "<br/>";
foreach($assign as $disp)
{
   echo  "return from repeat1:".$disp . "<br/>";                            
}       
 

Code: Select all

 
echo "function repeat2" . "<br/>";
function repeat2($reg,$search)
{
    $result1=preg_match_all($reg,$search,$match);
    if ($result1==0) 
    {
          $output= "value not found";
    }
    else
    {
         //echo "preg_match:result not found";
         $output="value found";
    }
    //return $match[0];
    echo "repeat2:" .$output . "<br/>";
    return $match[0];
}   
$assign=repeat2("/[a-z]/","asfaqwqeq"); 
foreach($assign as $disp)
{
   echo "return from repeat2:".$disp . "<br/>";                         
}                                                                        
?>
 
Output1:
function repeat1
repeat1:value found
return from repeat1:9
return from repeat1:Array




Output2
function repeat2
repeat2:value found
return from repeat2:a
return from repeat2:s
return from repeat2:f
return from repeat2:a
return from repeat2:q
return from repeat2:w
return from repeat2:q
return from repeat2:e
return from repeat2:q


This is what i require
From the first function ,i need the following output:

function repeat1
repeat1:value found
return from repeat1:9
return from repeat1:a
return from repeat1:s
return from repeat1:f
return from repeat1:a
return from repeat1:q
return from repeat1:w
return from repeat1:q
return from repeat1:e
return from repeat1:q


i.e the main problem am having is that am unable to display the value of match[0] in function
repeat1
Post Reply