Myguel
Myguel

Reputation: 1

Invisible characters are disrupting strpos(); how can I sanitize the strings?

It seems like I'm having an issue with strpos() For some reason, it's always returning false when it should not.

This is where the issue occur :

if ( strpos( $attachment_url, $upload_dir_paths['baseurl'] ) !== false ) {
    //some code here
}

I've made a var_dump of $attachment_url and $upload_dir_paths['baseurl'] and compared both values to make sure that the "needle" was in the "haystack" and in fact it was.

var_dump($upload_dir_paths['baseurl']);    //needle
var_dump($attachment_url);                 //haystack

Results of the var_dump above :

string(39) "//localhost:3000/wp-content/uploads"
string(67) "//localhost:3000/wp-content/uploads/2016/10/stunning-photograph.jpg"

Does anyone knows why the if statement is always returning false?

Upvotes: 0

Views: 521

Answers (1)

Ad5001
Ad5001

Reputation: 753

I think the problem comes from the variable $upload_dir_paths['baseurl'].
The string "//localhost:3000/wp-content/uploads" is composed of 35 characters unlike the 39 of your question.
I think that there are some "hidden" characters that cannot be rendered by your browser/your terminal.

How to solve the problem?
Maybe try the function "trim" on the variable $upload_dir_paths['baseurl']. It should solve the problem.

Upvotes: 1

Related Questions