Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

72 users online



Checking to see if a phrase exists in a string/variable.

Checking to see if a phrase exists in a string/variable.

Currently viewing this thread: 1 (0 members and 1 guests)


DigitalAssassin

DigitalAssassin

Status: Offline!

Checking to see if a phrase exists in a string/variable.

Okies, I am asking here, as I have looked all over the 'net (Google is considered "all of the 'net", right?) and I have been unable to find it. I think that it is a simple thing, so here goes.

Is it possible to make it so that the script or whatever checks if the variable starts with "http" or something like that. If it does, it leaves it as it is, while if it doesn't, it will automatically insert it for me.

Sorry if this seems like non-coherent babbling, but I am tired. (its 1.38AM here.)

Thanks in advance,

___________________

Sasha Yee
AKA
Digit@l_Assassin

http://www.imazine.com.au/SashWeb/images/avatars/snake_skull.jpg
SashWeb: Now Online!

sabrina

sabrina

what could you possibly want to know about me?
Status: Offline!

Give this a look: http://www.php.net/manual/en/function.strpos.php
I think it'll help you.

DigitalAssassin

DigitalAssassin

Status: Offline!

thanks for giving me the starter, but for the life of me, I can't figure out what is wrong with this script. Damn need for sleep...

I'd go to sleep, but I really want to see this MGS3 trailer...

Anyways, if you can point out the errors of my ways, I would greatly appreciate it.

PHP:

<?PHP

$needle 
'http://';
$string[1] = 'www.tutorialforums.com';
$string[2] = 'http://www.tutorialforums.com';
$n 2;

$i 1;
while (
$n >= $i) {
    
$strpos strpos($string[$i], $needle0);
    if(!
$strpos) {
        echo 
$needle ' was found in ' $string[$i];
        echo 
'<br />';
        echo 
'it was found at position ' $strpos;
    }
    else {
        echo 
$needle ' was not found in ' $string[$i];
        echo 
'<br />';
    }
    
$i++;
}

?>

___________________

Sasha Yee
AKA
Digit@l_Assassin

http://www.imazine.com.au/SashWeb/images/avatars/snake_skull.jpg
SashWeb: Now Online!

Last edited by DigitalAssassin, August 24th, 2004 01:46 AM (Edited 1 times)

sabrina

sabrina

what could you possibly want to know about me?
Status: Offline!

You have to compare on types as well. Otherwise 0 and false would send you to the same branch of your if.

I'd do something like

PHP:

<?php

// $s holds your string
if (strpos($s,'http://') !== 0$s 'http://'.$s;

?>


this would add http:// to the beginning of the string if it doesn't already begin with it (including if it occurs inside the string elsewhere)

Last edited by sabrina, August 24th, 2004 03:02 AM (Edited 1 times)

Ynhockey

Ynhockey

Neverside Newbie
Status: Offline!
Quote:

You have to compare on types as well. Otherwise 0 and false would send you to the same branch of your if.

That's why I recommend substr() or strstr() to newbies Wink

I heard it's slower, but I don't see how it could be if he only wants to check whether it's right in the beginning. Example:

if (substr($haystack, 0, 7) == 'http:\\\') { do whatever }

___________________

Learn HTML

Rad

Rad

thinking of something witty to put here
Status: Offline!
Quote:

Originally posted by Ynhockey
That's why I recommend substr() or strstr() to newbies

If someone is not capable of understanding the difference of 0 and false in the case of strpos(), they should not be learning PHP in the first place. It's really not that hard to figure out. Strpos can possibly return a position of 0 for the match, but 0 is converted to false automagically in PHP, so we keep it from thinking 0 is false by checking with the not identical operator (!==)

PHP:

<?php

$blah 
strpos('blah''ah') !== false;

?>

Ynhockey

Ynhockey

Neverside Newbie
Status: Offline!

Well, most n00bs don't read PHP.net so they can't understand this for the 1st time. Moreover, even if you're an experienced programmer this is a new thing, because in C for example, if (0 == FALSE) will return FALSE, while in PHP it returns TRUE. So, in both cases (good programmer or n00b), this is something new. Besides, in this particular case, substr() is exactly the same.

___________________

Learn HTML

DigitalAssassin

DigitalAssassin

Status: Offline!

Thanks a lot everyone...

And Radley, sorry I was tired at 3am in the morning, I'll try not to do it again.

___________________

Sasha Yee
AKA
Digit@l_Assassin

http://www.imazine.com.au/SashWeb/images/avatars/snake_skull.jpg
SashWeb: Now Online!

Rad

Rad

thinking of something witty to put here
Status: Offline!
Quote:

Originally posted by DigitalAssassin
Thanks a lot everyone...

And Radley, sorry I was tired at 3am in the morning, I'll try not to do it again.

No, it's not your fault, and I wasn't referring to you either. I'm sure you're fully capable of understanding how strpos() works.

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0094 seconds.