[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2596
  • Last Modified:

Drupal 7 htmlspecialchars expects parameter 1 to be string

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
sbayrak
Asked:
sbayrak
  • 3
  • 2
1 Solution
 
Julian HansenCommented:
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
 
sbayrakAuthor Commented:
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
 
Julian HansenCommented:
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
 
sbayrakAuthor Commented:
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
 
Julian HansenCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now