Jump to content


Photo

stristr oddities...


  • Please log in to reply
3 replies to this topic

#1 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 08 May 2006 - 08:18 PM

When using something like:

if(stristr("$file","¡AP: News¡") )

I would expect it to only hit true on an exact hit, ¡AP: News¡, however it seems to ignore the ¡ at the beginning and end and return true on something like ¡AP: News Text¡

Is there any way to ensure that it only returns true on that exact string with the ¡ at the beginning and end?

#2 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 09 May 2006 - 02:49 PM

After experiementing a bit more looks like it acts in an unintended way. I have:

if(stristr("$word_scan","$key_word") ) {
     $contents = str_replace("¿0a¡<br>¿0a¡¡¡20", "¿0a¡<br>¿0a¡¡¡19", $contents);}

I *THOUGHT* the if statement would go to the first occurence of $key_word, and then soon as it finds it, have the file pointer go to the first occurence of str_replace and replace it.

However, it appears as though as long as anything matches the $key_word, it rewrites ever occurance of str_replace within $contents.

Any ideas how I can rework it so that it will do how I thought it would do?



#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 09 May 2006 - 07:37 PM

Not sure if I have gone about this the long way, but this is the idea that first popped into my head...
<?php
$needle = "fox";
$haystack = "The quick brown fox jumps over the lazy dog - What a great fox he is!";
$replace = "elephant";

if(strpos($haystack,$needle)) {
    $pos = strpos($haystack,$needle); 
    $nlen = strlen($needle);
    $tlen = strlen($haystack);
    $newhaystack = substr($haystack,0,$pos).$replace.substr($haystack,($pos+$nlen),$tlen);
    echo $newhaystack;
} else {
    echo "$needle not found in $haystack.";
}
?>


#4 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 10 May 2006 - 04:09 PM

Thanks, that's an interesting way of doing it, I'll play with it and see how it works out :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users