Warning: strpos(): Empty needle in /XXX/post.php on line XXXX - php

I have such a prolem since I had to change the server provider.
I was porting a few wordpress based sites using the Duplicator plugin. Now wherever in the content of the page I use the tag, it returns an error
Warning: strpos(): Empty needle in /XXX/post.php on line XXXX
It's about the code:
function is_local_attachment($url) {
if (strpos($url, home_url()) === false) {
return false;
}
if (strpos($url, home_url('/? attachment_id =')) ! == false) {
return true;
}
$id = url_to_postid($url);
if ($id) {
$post = get_post($id);
if ('attachment' == $post-> post_type) {
return true;
}
}
return false;
}
and more specifically about the line:
if (strpos($url, home_url()) === false) {
Has anyone had such a case and knows how to solve it?
Everything was fine before the server switch. And I wish it would continue to be the case. I'd rather not fix the bug, but find and eliminate what led to it.
The only thing that comes to mind is whether I didn't have Site Address URL on the previous server, I don't have to have it on the new one - the field is empty.

The easiest way to solve this is to put a "guard clause" in. Something like this:
function is_local_attachment($url) {
$homeUrl = home_url();
if (empty($home_url)) {
return;
}
. . .
I don't know what home_url() returns but obviously it can return an empty string and that is causing you a probelm.
If you are using PHP 7.1 or higher you could use something like:
if (strpos($url, home_url()??' ') === false)
Personally, I wouldn't because it's more difficult to read, but it would solve your problem.
Cheers! :)
=C=

Related

PHP conditional statement is not working

I have a statement that checks the page's url and marks up a page accordingly, but it only works when my if statement has one option to check for.
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
<?php if (strpos($url, 'events/eventname')!= false) { ?>
~markup~
<? } ?>
If I modify it to check for two possible urls...
<?php if (strpos($url, 'events/eventname')!= false) { ?>
~markup~
<? }else if (strpos($url, 'events/othereventname')!= false) { ?>
~markup~
<? } ?>
... the page won't load. I must be missing something obvious- can someone tell me what is wrong with this function?
*edit: Since it was requested I have included the $url variable and more specific url examples
strpos returns 0 when search substring is in the beginning of the query string. You can replace != to !== to make it work - otherwise php is internally transforming false to zero, which leads to incorrect comparison result.
For example:
<?php
var_dump(strpos('aaa', 'a'));
echo var_dump(strpos('aaa', 'a') === false);
echo var_dump(strpos('aaa', 'a') == false);
Try to use !== comparison just just in case string is at position 0.
Another syntax problem is else if, while you should use elseif.
Try also changing short php tag <? to full one <?php.
Rather than using the strpos() you can get the request uri which is anything after the domain name (ie: www.example.com/foo/bar would give you /foo/bar).
$url = $_SERVER['REQUEST_URI'];
if($url == "/foo/bar") {
// markup
} elseif($url == "/bar/foo") {
// markup
} else {
// markup
}

Strpos always gives true

I have two type of links which are strings taken from database:
http://www.website.com/anything-else.html
www.website.com/anything-else.html
I need ALL links to be displayed with http:// no matter what so Im using this simple code to determine whether link has http in it and if not add it:
if (strpos($links, 'http') !== true) {
$linkai = 'http://'.$links;
}
The problem is, it is adding http:// to any link no matter if it has it or not.
I tried ==false ect. Nothing works. Any ideas?
Try this
if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}
In strpos documentation says return value not Boolean always.
"Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."
$arrParsedUrl = parse_url($links);
if (!empty($arrParsedUrl['scheme']))
{
// Contains http:// schema
if ($arrParsedUrl['scheme'] === "http")
{
}
// Contains https:// schema
else if ($arrParsedUrl['scheme'] === "https")
{
}
}
// Don't contains http:// or https://
else
{
$links = 'http://'.$links;
}
echo $links;

if http:// is in string then leave it, else if not add it

I have a input that you enter a URL, i basically want to write some php that says if the domain containts "http://" then leave it be, else if not then add it to the beginning.
This is what I have so far...
$domain = $_POST["domain"];
if (strpos($domain, "http://")) {
return $domain;
} else {
$domain = "http://" . $domain;
}
This doesnt seem to work..
it doesnt add the http:// on if it doesnt contain http://.
you forgot to return $domain.
$domain = $_POST["domain"];
if (strpos($domain, "http://") !== false) {
return $domain;
} else {
return "http://" . $domain;
}
Since the string starts with http://, strpos will return 0, which will evaluate to false.
Change the if statement to:
if(strpos($domain, "http://") !== FALSE){
"http://" then leave it be, else if not then add it to the beginning.
How about adding adding it regardless? I find that to be easier:
<?php
$url = 'http://www.google.com';
echo 'http://' . preg_replace( '~^http://~', '', $url );
read manual:
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please read
the section on Booleans for more
information. Use the === operator for
testing the return value of this
function.
That is because strpos will return the location of the string, within the string.
In your url, that is 0. Which equals to false. Make it a strict check - add === false.
if (strpos($domain, "http://") !== false) {
//return substr($domain,7); Thanks Rocket.
return $domain;
} else {
return "http://" . $domain;
}
I know this is a bit late to the party, but I prefer this approach:
if (!preg_match('#^http[s]{0,1}://#', $input)) {
$input = 'http://' . $input;
}
This will preserve a https:// address, and not have you ending up with http://https://www.mysite.com. You could also further edit it to strip out https:// if you had a rule for not using https addresses.
I know the original question didn't ask for this, but I think it's important to consider in most situations, and will hopefully help someone else who comes looking.
Use caution when using strpos(). It will return 0 when 'http://' is found at the beginning of the string, causing your if statement to fail unexpectedly. You will want to check the type of the return to be sure:
$domain = $_POST["domain"];
if (FALSE !== strpos($domain, "http://")) {
return $domain;
} else {
return "http://" . $domain;
}

How do I check whether the string $_GET["s"] has ++++ in it?

I can also call $_GET["s"] with get_search_query() which outputs a string.
I tried a lot of codes that were supposed to work but none did. Most give true whatever I do. Isn't there a simple way to do this? Thanks
Assuming you mean the literal string "++++"...
if (strpos($_GET['s'], '++++') !== false) {
// there, do something
} else {
// not there, do something else
}
Reference: http://php.net/strpos
if( strpos( $_GET['s'], '++++' ) !== false ) {
// has
} else {
// doesn't have
}
I'm not sure quite what's troubling you, or if I'm misunderstanding the question, but surely strpos is what you need:
if (strpos($_GET["s"],'++++') !=== false) {
// the string '++++' was found
}
Edited to fix 'strpos' (nee 'strpoa') typo...oops! With thanks #Jonah Bron =)

i have big problem in HTTP_REFERER

now i make simple system in my site and this its code
if(stc($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])){
//download directly
}else{
//open page first
}
function stc($haystack, $needle, $offset=0) {
return strpos(strtoupper($haystack), strtoupper($needle), $offset);
}
if any one enterd the link from my site its download directly ok
and if from any other sites its open a page
now its working great
but if any one installed any downloader such as inernet download manager
its make the link directly not going to page first i think because its make HTTP_REFERER null
now how i can doing the system like rapidshare.com
strpos can return 0.... 0 as in the pos a needle can be (at the first postion in the haystack, first position in a string is 0).
strpos() return's false if the needle was not found.
0 & false are both the same in if statements...
$test = 0;
if (!$test) echo "0 is like false";
$test2 = false;
if (!$test2) echo "This one is also false..";
You have to make sure it's false or 0, this you can do with: ===
if (strpos() === false) echo "It's not found for sure!";
So your code becomes:
if(stc($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false){
//download directly
}else{
//open page first
}

Categories