Jump to content

Archived

This topic is now archived and is closed to further replies.

Michael4172

stristr oddities...

Recommended Posts

When using something like:

[code]
if(stristr("$file","¡AP: News¡") )
[/code]

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?

Share this post


Link to post
Share on other sites
After experiementing a bit more looks like it acts in an unintended way. I have:

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

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?

Share this post


Link to post
Share on other sites
Not sure if I have gone about this the long way, but this is the idea that first popped into my head...
[code]<?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.";
}
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.