Gossamer Forum
Home : General : Internet Technologies :

String Operators (PHP)

Quote Reply
String Operators (PHP)
Hello.

I just want to double check with those PHP experts out there in terms of using the . to find out whether a string "contains" some string.

I did read the following web page:

http://us4.php.net/...age.operators.string

I am using the following codes in one of my PHP scripts:

Code:
if ($destination . "sometext") {

It seems to do what I want, but not sure if that's the most efficient method of finding out whether a string contains some text.

Thanks in advance.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] String Operators (PHP) In reply to
I've never seen the concatenation operator used in that way, but I guess if it works, why not?

I would normally just use the strstr function for this.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [Stealth] String Operators (PHP) In reply to
I don't think that's what you want. "." will concat two strings to together (join them into one string). It won't tell you if one contains the other.

I think you want strstr like recommended by hennagaijin .

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] String Operators (PHP) In reply to
Thanks, hennagaijin and Alex.

Yes, strstr function seems to be a more efficient method of doing it. I'll take a closer look at the PHP site web page and see if I can't improve the codes I am using...

Thanks.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] String Operators (PHP) In reply to
Welp, strstr function didn't really do the trick for me...so I used strpos function:

Code:
$pos = strpos($destination, 'sometext');
if ($pos == true) {
OTHER CONDITIONS
}

works just fine....you were correct, Alex, the . string operator didn't really check to see if the text was within the string.
Quote Reply
Re: [Stealth] String Operators (PHP) In reply to
Could also use preg_match();

Code:
$string "see if we can find This";

if (preg_match("/find this/i",$string,$var)) {
do_this();
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] String Operators (PHP) In reply to
If you don't actually need to use regex matching, the straight-up string matching functions should be more efficient.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] String Operators (PHP) In reply to
Good points...yes, I don't need to use regular expression pattern matching...just checking that the redirection var string ($direction) contains a certain string of text in order to check other conditions...
========================================
Buh Bye!

Cheers,
Me