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

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 =)

Related

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

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=

Php - Search a string in cookie

I set cookie with php, using
setcookie("name", "John Mayer", time()+31556926 , "/");
After, I want to search Cookie value with php to check if it includes a value, using
if (strpos($_COOKIE["name"],"Mayer")) {
However, it always returns false. Is it not allowed to check cookie value with php ? If so is there anyway to check it ?
Actually strpos returns a index so,if string matched from initial then it returns 0 which is zero and considered as false. It returns -1 if search text didnot matched, so you can make a condition like-
if (strpos($_COOKIE["name"],"Mayer")>=0)
{
}
// or strict comparison with false
if (strpos($_COOKIE["name"],"Mayer")!==false)
{
}
Change it to:
if (strpos($_COOKIE["name"],"Mayer") !== false) {
PHP strpos manual
You edit your code.
if(isset($_COOKIE["name"]) && !empty($_COOKIE["name"])) {
if (strpos($_COOKIE["name"], 'Mayer') !== false) {
echo 'exists';
}
}
check if its set and proceed
if(isset($_COOKIE["name"])) {
if (strpos($_COOKIE["name"], 'Mayer') !== false) {
// it exists
echo 'true';
}
}

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;

How to check if an include() returned anything?

Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}

To manipulate URL based on coming URL by PHP

How can you manipulate the destination URL by the name of the starting url?
My URL is
www.example.com/index.php?ask_question
I do the following manipulation in the destination URL
if ($_GET['ask_question']) {
// Problem HERE, since if -clause is always false
if ( $login_cookie_original == $login_cookie )
{
include "/codes/handlers/handle_login_status.php";
header("Location: /codes/index.php?ask_question");
die("logged in - send your question now");
}
}
if (isset($_GET['ask_question'])) {
...
If you did a print_r() of $_GET you would see
Array
(
[ask_question] =>
)
which shows that ask_question is set, but is empty, so it tests false.
$location = "test.php";
if(isset($_SERVER['QUERY_STRING']))
{
header("Location:".$location . "?" . $_SERVER['QUERY_STRING']);
}
else
{
header("Location:".$location);
}
I think you want to replace that with:
if (isset($_GET['ask_question'])) {
Which will only be true if it's contained in the URL.
you could possibly check the $_SERVER['QUERY_STRING'] variable to see if it contains 'ask_question'
edit: fixed the typo
You can retrieve values after the question mark by using the $_GET super global. For the example ?ask_question=true.
//is ask_question true?
if($_GET['ask_question'] == 'true') {
echo 'ask_question is true';
} else {
echo 'ask_question is not true';
}
For variables without values (like ?hello), use $_GET in such way:
if(isset($_GET['hello'])) {
echo 'hello is there';
} else {
echo 'hello is not there';
}
You've asked a lot of very basic questions about PHP and you don't seem to have a grasp on how the language works. I suggest giving the documentation a good read before your next question.
PHP Documention Home
PHP: Variables From External Sources

Categories