See if one string contains another string
I use the following code to check if a string is the session username at the moment.
if($_SESSION['username'] == $home || $_SESSION['username'] == $away)
I am looking to change that to see if the string includes the session username, not specifically IS the username.
Is there a way to do this?
Thankyou
Edit :
Say the string is "Username1 and Username2", I will "Username1" to be found.
I have done the following:
if( ( strpos($home, $_SESSION['username']) !== false) || ( strpos($away, $_SESSION['username']) !== false) )
That doesnt appear to have worked though!
php
add a comment |
I use the following code to check if a string is the session username at the moment.
if($_SESSION['username'] == $home || $_SESSION['username'] == $away)
I am looking to change that to see if the string includes the session username, not specifically IS the username.
Is there a way to do this?
Thankyou
Edit :
Say the string is "Username1 and Username2", I will "Username1" to be found.
I have done the following:
if( ( strpos($home, $_SESSION['username']) !== false) || ( strpos($away, $_SESSION['username']) !== false) )
That doesnt appear to have worked though!
php
add a comment |
I use the following code to check if a string is the session username at the moment.
if($_SESSION['username'] == $home || $_SESSION['username'] == $away)
I am looking to change that to see if the string includes the session username, not specifically IS the username.
Is there a way to do this?
Thankyou
Edit :
Say the string is "Username1 and Username2", I will "Username1" to be found.
I have done the following:
if( ( strpos($home, $_SESSION['username']) !== false) || ( strpos($away, $_SESSION['username']) !== false) )
That doesnt appear to have worked though!
php
I use the following code to check if a string is the session username at the moment.
if($_SESSION['username'] == $home || $_SESSION['username'] == $away)
I am looking to change that to see if the string includes the session username, not specifically IS the username.
Is there a way to do this?
Thankyou
Edit :
Say the string is "Username1 and Username2", I will "Username1" to be found.
I have done the following:
if( ( strpos($home, $_SESSION['username']) !== false) || ( strpos($away, $_SESSION['username']) !== false) )
That doesnt appear to have worked though!
php
php
edited Jul 28 '11 at 19:19


user7116
54.7k15124160
54.7k15124160
asked Jul 30 '10 at 15:02
sark9012sark9012
2,410124780
2,410124780
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
One way, of many, would be to use the strpos()
function, which the documentation says is the fastest way to just determine if a substring occurs within a string.
if (strpos($_SESSION['username'],$home) !== false)
The format of strpos is strpos(*haystack*, *needle*)
. So, the above would be true if $_SESSION['username']
is Username1 and $home
is Username1 and Username2.
If you actually need the substring back (rather than a position), strstr()
is a good way to go.
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
|
show 2 more comments
This would work
$username = 'theusername';
if(strpos($username,$_SESSION['username']) !== false) {
// contains username
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3372722%2fsee-if-one-string-contains-another-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way, of many, would be to use the strpos()
function, which the documentation says is the fastest way to just determine if a substring occurs within a string.
if (strpos($_SESSION['username'],$home) !== false)
The format of strpos is strpos(*haystack*, *needle*)
. So, the above would be true if $_SESSION['username']
is Username1 and $home
is Username1 and Username2.
If you actually need the substring back (rather than a position), strstr()
is a good way to go.
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
|
show 2 more comments
One way, of many, would be to use the strpos()
function, which the documentation says is the fastest way to just determine if a substring occurs within a string.
if (strpos($_SESSION['username'],$home) !== false)
The format of strpos is strpos(*haystack*, *needle*)
. So, the above would be true if $_SESSION['username']
is Username1 and $home
is Username1 and Username2.
If you actually need the substring back (rather than a position), strstr()
is a good way to go.
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
|
show 2 more comments
One way, of many, would be to use the strpos()
function, which the documentation says is the fastest way to just determine if a substring occurs within a string.
if (strpos($_SESSION['username'],$home) !== false)
The format of strpos is strpos(*haystack*, *needle*)
. So, the above would be true if $_SESSION['username']
is Username1 and $home
is Username1 and Username2.
If you actually need the substring back (rather than a position), strstr()
is a good way to go.
One way, of many, would be to use the strpos()
function, which the documentation says is the fastest way to just determine if a substring occurs within a string.
if (strpos($_SESSION['username'],$home) !== false)
The format of strpos is strpos(*haystack*, *needle*)
. So, the above would be true if $_SESSION['username']
is Username1 and $home
is Username1 and Username2.
If you actually need the substring back (rather than a position), strstr()
is a good way to go.
edited Nov 21 '18 at 3:22


Luke Peterson
5,94783440
5,94783440
answered Jul 30 '10 at 15:04
Mark BiekMark Biek
91.9k50144193
91.9k50144193
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
|
show 2 more comments
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
+1, but please add the link to the PHP doc.
– Bob Fincheimer
Jul 30 '10 at 15:06
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Best to add !== FALSE, your example fails if the string is in the beginning.
– Blizz
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
Wouldn't this fail if the session username is at position 0
– Neil Aitken
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
@Bob, Thanks for the +! Both function names are linked to the PHP docs.
– Mark Biek
Jul 30 '10 at 15:07
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
Good point Blizz & Neil. I've updated accordingly.
– Mark Biek
Jul 30 '10 at 15:11
|
show 2 more comments
This would work
$username = 'theusername';
if(strpos($username,$_SESSION['username']) !== false) {
// contains username
}
add a comment |
This would work
$username = 'theusername';
if(strpos($username,$_SESSION['username']) !== false) {
// contains username
}
add a comment |
This would work
$username = 'theusername';
if(strpos($username,$_SESSION['username']) !== false) {
// contains username
}
This would work
$username = 'theusername';
if(strpos($username,$_SESSION['username']) !== false) {
// contains username
}
edited Jan 9 '15 at 16:47
answered Jul 30 '10 at 15:05
Neil AitkenNeil Aitken
6,86633440
6,86633440
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3372722%2fsee-if-one-string-contains-another-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown