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:


$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


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.

if ( strpos( join( $_GET ), $keyword ) )
  ...stuff...

If I understand what you want correctly…

But what I think you really want is:


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

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.

$_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() ? ;))

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.

Lets say you have a form containing a text field such as this into which the user simply types #feed


<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


$_POST['keyword'];  

var_dump() that and you will see it contains


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.


$keyword = "#feed";

in my above example that is the same thing as


$keyword = $_POST['keyword'];

Anyhow, I think that is the point you are alluding to…

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:

myurl.com/?string=do not #feed the animals

if you do:


<?php

echo $_GET['string'];

?>

PHP echos out the text

"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:

myurl.com/?string=do not !feed the animals

if you do:


<?php

echo $_GET['string'];

?>

PHP echos out the text

"do not !feed the animals"

Hope this helps! :slight_smile:

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 :slight_smile:

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.

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.

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 :slight_smile: