how to return the value when the match is a subpattern?

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

Moderator: General Moderators

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

how to return the value when the match is a subpattern?

Post 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.
Last edited by swetha on Thu Oct 02, 2008 7:00 am, edited 4 times in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: need output for this

Post 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?
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to return the value when the match is a subpattern?

Post by prometheuzz »

Edit: nvm
Last edited by prometheuzz on Sat Oct 04, 2008 3:33 am, edited 1 time in total.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post 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?
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post by swetha »

pls help me for this
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to return the value when the match is a subpattern?

Post by prometheuzz »

Edit: nvm
Last edited by prometheuzz on Sat Oct 04, 2008 3:35 am, edited 1 time in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to return the value when the match is a subpattern?

Post by prometheuzz »

Edit: nvm
Last edited by prometheuzz on Sat Oct 04, 2008 3:32 am, edited 1 time in total.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post by swetha »

how do i pass the return value in the statement
return match[]?.kindly check the code which i have posted.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to return the value when the match is a subpattern?

Post by prometheuzz »

Edit: nvm
Last edited by prometheuzz on Sat Oct 04, 2008 3:31 am, edited 1 time in total.
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post by swetha »

This does not work for subpatterns
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to return the value when the match is a subpattern?

Post by prometheuzz »

Edit: nvm
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post by swetha »

what does this Edit:nvm mean?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to return the value when the match is a subpattern?

Post by prometheuzz »

swetha wrote:what does this Edit:nvm mean?
'nvm' means 'never mind'
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: how to return the value when the match is a subpattern?

Post by swetha »

pls help me with returning the value of the subpatterns too in the above code.
Post Reply