简体   繁体   中英

php replacing string error

I am trying to delete a string from a string, but the result of strstr is not finding the string. I will try to be as clear as I can here....

The problem is strpos() is not finding $deletTabHTML. I have alerted it in ajax and it is exactly the same as a line in the commonHTML, but obviously it isn't for some reason I cannot figure out. I am assuming I am missing something 'invisible'? My script works if I hardcode the html to be deleted, so the overall script works.

here is the php:

$commonHTML = file_get_contents($url);
if (!empty($_POST['action']) && $_POST['action'] == 'deleteTab') {
    $deletTabHTML = trim($_POST['theHTM']);
    if(strpos($commonHTML, $deletTabHTML) !== false) {
        $is_deleted="deleted";
    }else{
        $is_deleted="NOT deleted, ERROR:".$deletTabHTML;
    }
    echo '{"is_deleted":"' . $is_deleted . '"}';
    return;
}

MORE INFO: jQuery is getting an element from the dom and sending it to a php script which is opening a file and deleting the element:

<li id="contact"><a href="#contact" rel="ajax">Contact</a></li>

The data returned to ajax is:

<li id="contact"><a href="#contact" rel="ajax">Contact</a></li>

but for some reason it is not finding it. Thos were copied and pasted from the actual file and a javascript alert. They look exactly the same.

I hope that is enough info.

strpos is case sensitive

Try

stripos()

Aside from attempting to make the text search case insensitive, you also might want to make sure that it contains no unicode characters by using utf8_decode() on it first.

Lastly, it couldn't hurt to do some sanity checks on $_POST['theHTM'] before attempting to use it. (It looks like it's missing an L at the end, but it's also worth using isset() to verify that it actually exists.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM