Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for encodings different than UTF-8 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ abstract class OpenGraphProtocolObject {

/**
* Output the object as HTML <meta> elements
*
* @param string $encoding
*
* @return string HTML meta element string
*/
public function toHTML() {
return rtrim( OpenGraphProtocol::buildHTML( get_object_vars($this), static::PREFIX ), PHP_EOL );
public function toHTML($encoding = 'UTF-8') {
return rtrim( OpenGraphProtocol::buildHTML( get_object_vars($this), static::PREFIX, $encoding ), PHP_EOL );
}

/**
Expand Down
21 changes: 13 additions & 8 deletions open-graph-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ class OpenGraphProtocol {
/**
* Build Open Graph protocol HTML markup based on an array
*
* @param array $og associative array of OGP properties and values
* @param array $og associative array of OGP properties and values
* @param string $prefix optional prefix to prepend to all properties
* @param string $encoding encoding for htmlspecialchars
*
* @return string
*/
public static function buildHTML( array $og, $prefix=self::PREFIX ) {
public static function buildHTML( array $og, $prefix=self::PREFIX, $encoding = 'UTF-8' ) {
if ( empty($og) )
return;

Expand All @@ -150,14 +153,14 @@ public static function buildHTML( array $og, $prefix=self::PREFIX ) {
if ( is_object( $content ) )
$content = $content->toArray();
if ( empty($property) || !is_string($property) )
$s .= static::buildHTML( $content, $prefix );
$s .= static::buildHTML( $content, $prefix, $encoding );
else
$s .= static::buildHTML( $content, $prefix . ':' . $property );
$s .= static::buildHTML( $content, $prefix . ':' . $property, $encoding );
} elseif ( !empty($content) ) {
$s .= '<meta ' . self::META_ATTR . '="' . $prefix;
if ( is_string($property) && !empty($property) )
$s .= ':' . htmlspecialchars( $property );
$s .= '" content="' . htmlspecialchars($content) . '">' . PHP_EOL;
$s .= ':' . htmlspecialchars( $property , ENT_COMPAT, $encoding );
$s .= '" content="' . htmlspecialchars($content, ENT_COMPAT, $encoding ) . '">' . PHP_EOL;
}
}
return $s;
Expand Down Expand Up @@ -445,10 +448,12 @@ public static function is_valid_url( $url, array $accepted_mimes = array() ) {
/**
* Output the OpenGraphProtocol object as HTML elements string
*
* @param string $encoding
*
* @return string meta elements
*/
public function toHTML() {
return rtrim( static::buildHTML( get_object_vars($this) ), PHP_EOL );
public function toHTML($encoding = 'UTF-8') {
return rtrim( static::buildHTML( get_object_vars($this), static::PREFIX, $encoding ), PHP_EOL );
}

/**
Expand Down