code using reg expression

Discussion in 'PHP' started by anurag.sharma, Jun 24, 2008.

  1. #1
    I have a text box in my webpage where users can enter the data. They will be entering it in a specific format. I wish to parse the content a extract few words from the submission


    If the user type "I am xyz. I am from abc."

    I want to get the words xyz and abc using reg expression in php. Further I want to redirect the user to another php page named abc.php and greet the user " Hello xyz" and so on (i, if the user enter "I am pqr. I am from sv"he should be redirected to sv.php and greet him "Hello pqr")

    I will be using post function in html to post the data. I will be very greatful if some one could help me by writing the code of the php code of the page to which I need to POST the data entered.

    Please help.
     
    anurag.sharma, Jun 24, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    preg_match('/I am (.*?). I am from (.*?)/',$_POST['text'],$match);
    print_r($match);
     
    matthewrobertbell, Jun 24, 2008 IP
  3. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have read about this in some tutorial. but don't know how to use it in a php file . if you don't mind cud write the code for the page.

    And how do I do the redirection stuff?

    Anyway thanks a lot for the response.
     
    anurag.sharma, Jun 24, 2008 IP
  4. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    header('Location: '.$match[2].'.php?name='.$match[1]);

    should work for redirection.
     
    matthewrobertbell, Jun 24, 2008 IP
  5. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am not getting the desired results it is simply displaying

    "Array"

    For your ref. here is the php code i used

    <?php
    preg_match('/I am (.*?). I am from (.*?)/',$_POST['text'],$match);
    print_r($match);
    ?>
    PHP:
    and my html form is

    <form method="POST" action="file.php">
      <p><textarea rows="10" name="text" cols="47"></textarea><input type="submit" value="Submit" name="text"></p>
    
    HTML:
    And when I added the headers then also it is not getting redirected. simply shows .ph/name=


    here is code i used

    <?php
    header('Location: '.$match[2].'.php?name='.$match[1]);
    preg_match('/I am (.*?). I am from (.*?)/',$_POST['text'],$match);
    print_r($match);
    ?>
    PHP:
    I am beginner in php . Pls help
     
    anurag.sharma, Jun 24, 2008 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    put the header line after the preg_match line
     
    matthewrobertbell, Jun 24, 2008 IP
  7. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    still getting error msg

    "The requested URL /.php was not found on this server."
     
    anurag.sharma, Jun 24, 2008 IP
  8. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    resolved thanks
     
    anurag.sharma, Jun 25, 2008 IP
  9. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #9
    what did you do wrong, please also post the working version of your code so others can learn from it ;)
     
    EricBruggema, Jun 25, 2008 IP