Jump to content

stristr oddities...


Michael4172

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?
Link to comment
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?

Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.