SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Location
    Accra, Ghana
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting a String from $_GET

    Hello,

    I'm still very new to PHP, but I hope I can use the right terminology to get my question across.

    I am writing a very simple program for an SMS code. When a user sends a message to the code we'll receive:

    https://www.myurl.com/message?from=xxx&to=xxx&text=xxx&date=xxx

    so we will receive from (users number), to(our shortcode), text (message text), and the date (date sent) in the form of a $_GET array.

    I need to check if there is a keyword in the $_GET['text']. So I understand I should use the function strpos(), but that searches a string and I have an array (Given it is an array of one thing).

    My initial instinct is to do this:
    Code:
    $message = $_GET['text'];
    $keyword = '#feed';
    		
    foreach($_GET as $message => $text){
      echo '' . $text . '';
    }
    to make the array a string (I'm thinking this doesn't actually do that?) and then

    Code:
    if (strpos($text, $keyword)) {
      //do what I should do with the messages with the $keyword
    }
    However, I get nothing back using that. It seems the IF never returns true.

    So something isn't right obviously. I'm guessing I am not making a string out of the text from the message? But don't really have a clue. I really appreciate the help this community provides, thanks in advance.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( strposjoin$_GET ), $keyword ) )
      ...
    stuff... 
    If I understand what you want correctly...

    But what I think you really want is:
    PHP Code:
    if ( strpos$_GET['text'], $keyword ) ) 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Location
    Accra, Ghana
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what my initial thought was, I should just be able to use the $_GET['text']. So I went back and looked at things after you confirmed that. It seems the issue is with my keyword, specifically the '#' part of it. If I say:

    $keyword = 'feed';

    I can get the if statements to run correctly, but if I use

    $keyword = '#feed';

    Then

    if ( strpos( $_GET['text'], $keyword ) )

    won't ever return true. I imagine this has to do with the #. I imagine it does something like comments out a part of the code? I am slowly figuring out how to deal with characters other than letters and numbers, but the amount of different rules and ways to escape certain characters is overwhelming whenever I google something. I'm sure I'll figure it out in time, but if someone has a suggestion to how to have my keyword by #feed, and the if statement still work, I'd really appreciate it.

    Thank you.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $_GET['text'] = "a string with #feed in it";

    $target "#feed";

    if( 
    strpos$_GET['text'], $target) ){

    echo 
    "$target is in the string";

    }

    var_dump(strpos$_GET['text'], $target) );
    // 15 
    Use var_dump() when your expectations are not being met.

    (maybe they should give var_dump() an alias, AmIGoingMad() ? )

  5. #5
    SitePoint Member
    Join Date
    Jun 2012
    Location
    Accra, Ghana
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the var_dump tip. I'm sure I'll use that endlessly from now on. And thanks for the MUCH needed humor haha.

    Using that I identified the problem. I want the user to text in a keyword #feed. But as of now for my program to pick up the keyword they have to text in '#feed'. How can I make the system pick up the keyword #feed. Not '#feed'?

    I'm declaring the keyword a variable by saying

    $keyword = '#feed';

    I hope that makes sense. I've been drinking a lot of coffee.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Lets say you have a form containing a text field such as this into which the user simply types #feed

    Code:
    <form method=POST action="search.php">
    <input type=text name="keyword" />
    ... etc
    Your form handler (search.php in this made up example) will access that incoming string as
    PHP Code:
    $_POST['keyword']; 
    var_dump() that and you will see it contains
    PHP Code:
    string (5#feed 
    You can then use that a string straight away, no need to add quotes to it.

    You only add the quotes yourself so that PHP knows you that a string is enclosed within the quotes.
    PHP Code:
    $keyword "#feed"
    in my above example that is the same thing as
    PHP Code:
    $keyword $_POST['keyword']; 
    Anyhow, I think that is the point you are alluding to...

  7. #7
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think using $_GET[] is not the best idea if you want to identify a keyword be prefixing it with an octothorpe (#). The reason being is in a url this refers to an anchor link within the page. Consider this:

    Code:
    myurl.com/?string=do not #feed the animals
    if you do:
    PHP Code:
    <?php

    echo $_GET['string'];

    ?>
    PHP echos out the text
    Code:
    "do not "
    This is because everything from after the # is seen as a reference to a link within the page and not part of the variable. You should consider using either POST_[] or using a different prefix to identify keywords. Choose a special character that is not used in forming URLs (off the top of my head, stay away from #, /, ., ?, $, %).

    Try using something like an exclamation mark to identify a keyword. For example:

    Code:
    myurl.com/?string=do not !feed the animals
    if you do:
    PHP Code:
    <?php

    echo $_GET['string'];

    ?>
    PHP echos out the text
    Code:
    "do not !feed the animals"
    Hope this helps!
    Last edited by iainjames88; Jul 28, 2012 at 12:02. Reason: Expanded answer

  8. #8
    SitePoint Member
    Join Date
    Jun 2012
    Location
    Accra, Ghana
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    james,

    That helps a lot down the line. I've figured out my earlier question, and with your help on that I think I should be able to do a good amount of work until something else comes up

    The reason I'm using GET though is because i am connecting to a short codes API. So they will pass users number, message, etc, through a get. So I have to receive it as a get. If that makes sense.

    Thanks everyone for the help.

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iainjames88 View Post
    I think using $_GET[] is not the best idea if you want to identify a keyword be prefixing it with an octothorpe (#). The reason being is in a url this refers to an anchor link within the page. Consider this:
    Using $_GET is not a problem in this case nor is using the pound sign from within a form. On submit the form will encode all values into URL save components this the pound sign is encoded so as not to be confused with the URL. The only time you have to worry about it is if you manually build the URL.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    SitePoint Member
    Join Date
    Jun 2012
    Location
    Accra, Ghana
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the helpful discussion guys.

    I just want to add I was thinking my if statements weren't being picked up because of single and double quotation kinda errors. The reality was I had to use (strpos($message, $keyword) !== FALSE). When I wasn't adding the '!== FALSE' the response would always be false because it would respond the position as 0, or FALSE.

    I hope that makes sense


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •