
August 24th, 2004
12:31 AM
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

SashWeb: Now Online!

August 24th, 2004
12:59 AM
what could you possibly want to know about me?
Status: Offline!

August 24th, 2004
01:28 AM
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
$needle = 'http://';
$string[1] = 'www.tutorialforums.com';
$string[2] = 'http://www.tutorialforums.com';
$n = 2;
$i = 1;
while ($n >= $i) {
$strpos = strpos($string[$i], $needle, 0);
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

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

August 24th, 2004
03:00 AM
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
// $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)

August 24th, 2004
05:38 AM
Neverside Newbie
Status: Offline!
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 
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

August 24th, 2004
06:34 AM
thinking of something witty to put here
Status: Offline!
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
$blah = strpos('blah', 'ah') !== false;
?>

August 24th, 2004
07:29 AM
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

August 24th, 2004
11:43 AM
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

SashWeb: Now Online!

August 24th, 2004
01:44 PM
thinking of something witty to put here
Status: Offline!
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.