Solved

Drupal 7 htmlspecialchars expects parameter 1 to be string

Posted on 2013-12-04
5
2,220 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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now