Solved

Drupal 7 htmlspecialchars expects parameter 1 to be string

Posted on 2013-12-04
5
2,291 Views
Last Modified: 2014-01-14
We're using Drupal 7.14.

We get "Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1572 of /usr/local/www/.../.../includes/bootstrap.inc)." when we access the "Views" pages that has "exposed filters" in the filter criteria. I think this warning fires when there're multiple exposed filters...  

We cannot upgrade Drupal version yet as a solution.

Views version is 7.x-3.7

# php -v
PHP 5.3.5 with Suhosin-Patch (cli) (built: Jan 17 2011 16:31:18)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

Thanks in advance.
0
Comment
Question by:sbayrak
  • 3
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39697554
This is appears to be a minor problem with Drupal that appears to be addressed in later vesions.

This article proposes a solution for getting around the warning by editing the bootstrap.inc file and modifying the check_plain function.

http://info4admins.com/warning-htmlspecialchars-expects-parameter-1-be-string-array-given-includesbootstrap-inc-line/

You might want to check this out as an interim solution. Source from above link included below for reference
function check_plain($text) {
static $php525;

if (!isset($php525)) {
$php525 = version_compare(PHP_VERSION, ’5.2.5', ‘>=’);
}
// We duplicate the preg_match() to validate strings as UTF-8 from
// drupal_validate_utf8() here. This avoids the overhead of an additional
// function call, since check_plain() may be called hundreds of times during
// a request. For PHP 5.2.5+, this check for valid UTF-8 should be handled
// internally by PHP in htmlspecialchars().
// @see http://www.php.net/releases/5_2_5.php
// @todo remove this when support for either IE6 or PHP < 5.2.5 is dropped.

if ($php525) {
//Org: return htmlspecialchars($text, ENT_QUOTES, ‘UTF-8');
//Fix:
return (preg_match(‘/^./us’, (string) $text) == 1) ? htmlspecialchars((string) $text, ENT_QUOTES, ‘UTF-8') : ”;

}
return (preg_match(‘/^./us’, $text) == 1) ? htmlspecialchars($text, ENT_QUOTES, ‘UTF-8') : ”;
}

Open in new window

0
 
LVL 1

Author Comment

by:sbayrak
ID: 39697887
I think this workaround was planned for the versions older than mine. We use Drupal 7.14 at the moment. So there're differences at that part of the bootstrap.inc code. But even so this doesn't mean that it won't work for our version.

Here's what goes on at line 1572 in my version. Only three lines;

function check_plain($text) {
  return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');       
}

Open in new window


I'm not a PHP expert but I replaced the relevant line similar as your workarround suggests. I'm testing it on the beta site now and I will test it for a while. Do you think that it will work as such?

function check_plain($text) {
// return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');      
  return (preg_match('/^./us', (string) $text) == 1) ? htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8') : '';           
}

Open in new window


Thanks.
0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39697934
If you look at the code posted the code commented out as

// Orig

Contains the line you have in yours - and is the same as mine - which is version 7.22

The code changes recommended will be fine for your version.

With regards to your question - I think you should include the other bits as well which basically setup a condition for the version of PHP you are using. Here is a stripped down version of the recommended change
function check_plain($text) 
{
  static $php525;

  if (!isset($php525)) {
    $php525 = version_compare(PHP_VERSION, '5.2.5', '>=');
  }

  if ($php525) {
    return (preg_match('/^./us', (string) $text) == 1) ? htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8') : '';
  }
  return (preg_match('/^./us', $text) == 1) ? htmlspecialchars($text, ENT_QUOTES, 'UTF-8') : ";
}

Open in new window

0
 
LVL 1

Author Comment

by:sbayrak
ID: 39699359
I see. My version is unnecessarily creative. I think I was prejudiced when I saw php525 as variables.

Now I'm testing your version as;

function check_plain($text) {
// Orig: return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
  static $php525;
  if (!isset($php525)) {
    $php525 = version_compare(PHP_VERSION, '5.2.5', '>=');
  }
  if ($php525) {
    return (preg_match('/^./us', (string) $text) == 1) ? htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8') : '';
  }  
  return (preg_match('/^./us', $text) == 1) ? htmlspecialchars($text, ENT_QUOTES, 'UTF-8') : '';
} 

Open in new window

0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39778608
Thanks for the poings - however could you provide more information as to why you assigned a B grade to this question?

Please refer to this article for more information http://support.experts-exchange.com/customer/portal/articles/481419
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question