PHP - Read three lines of remote html - php

I need to read three lines of a remote page using PHP. I'm using code from Jose Vega found here to read the title:
<?php
function get_title($url){
$str = file_get_contents($url);
if(strlen($str)>0){
$str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
return $title[1];
}
}
//Example:
echo get_title("http://www.washingtontimes.com/");
?>
When I plug in a URL, I want to extract the following information:
<title>TITLE HERE</title>
<meta property="end_date" content="Tue Aug 28 2018 03:59:59 GMT+0000 (UTC)" />
<meta property="start_date" content="Mon Aug 06 2018 04:00:00 GMT+0000 (UTC)" />
Outputs: $title, $start, $end
Displayed as a title with a link to URL, followed by Starts: ____, Ends: ____, preferably converted to simple dates
Bonus Question: How can I efficiently parse dozens of sites using this script? The sites are all ascending numerically. index.php?id=103 index.php?id=104 index.php?id=105
Displaying:
ID Title Start End
#103 TitleWithLink StartDate EndDate
#104 TitleWithLink StartDate EndDate
#105 TitleWithLink StartDate EndDate

Based on your question i guessed you want to read metadata.A part of the code i will suggest now has been taken from http://php.net/manual/en/function.get-meta-tags.php
.It works fine for this SO page so it will work fine for yours too.Of course you will need to adapt it a little to get your task done.
function getUrlData($url, $raw=false) // $raw - enable for raw display
{
$result = false;
$contents = getUrlContents($url);
if (isset($contents) && is_string($contents))
{
$title = null;
$metaTags = null;
$metaProperties = null;
preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );
if (isset($match) && is_array($match) && count($match) > 0)
{
$title = strip_tags($match[1]);
}
preg_match_all('/<[\s]*meta[\s]*(name|property)="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && count($match) == 4)
{
$originals = $match[0];
$names = $match[2];
$values = $match[3];
if (count($originals) == count($names) && count($names) == count($values))
{
$metaTags = array();
$metaProperties = $metaTags;
if ($raw) {
if (version_compare(PHP_VERSION, '5.4.0') == -1)
$flags = ENT_COMPAT;
else
$flags = ENT_COMPAT | ENT_HTML401;
}
for ($i=0, $limiti=count($names); $i < $limiti; $i++)
{
if ($match[1][$i] == 'name')
$meta_type = 'metaTags';
else
$meta_type = 'metaProperties';
if ($raw)
${$meta_type}[$names[$i]] = array (
'html' => htmlentities($originals[$i], $flags, 'UTF-8'),
'value' => $values[$i]
);
else
${$meta_type}[$names[$i]] = array (
'html' => $originals[$i],
'value' => $values[$i]
);
}
}
}
$result = array (
'title' => $title,
'metaTags' => $metaTags,
'metaProperties' => $metaProperties,
);
}
return $result;
}
function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
$result = false;
$contents = #file_get_contents($url);
// Check if we need to go somewhere else
if (isset($contents) && is_string($contents))
{
preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
{
if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
{
return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
}
$result = false;
}
else
{
$result = $contents;
}
}
return $contents;
}
$result = getUrlData('https://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html', true);
the output of print_r($result); is:
Array
(
[title] => file get contents - PHP - Read three lines of remote html - Stack Overflow
[metaTags] => Array
(
[viewport] => Array
(
[html] => <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">
[value] => width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0
)
[twitter:card] => Array
(
[html] => <meta name="twitter:card" content="summary"/>
[value] => summary
)
[twitter:domain] => Array
(
[html] => <meta name="twitter:domain" content="stackoverflow.com"/>
[value] => stackoverflow.com
)
[twitter:app:country] => Array
(
[html] => <meta name="twitter:app:country" content="US" />
[value] => US
)
[twitter:app:name:iphone] => Array
(
[html] => <meta name="twitter:app:name:iphone" content="Stack Exchange iOS" />
[value] => Stack Exchange iOS
)
[twitter:app:id:iphone] => Array
(
[html] => <meta name="twitter:app:id:iphone" content="871299723" />
[value] => 871299723
)
[twitter:app:url:iphone] => Array
(
[html] => <meta name="twitter:app:url:iphone" content="se-zaphod://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html" />
[value] => se-zaphod://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html
)
[twitter:app:name:ipad] => Array
(
[html] => <meta name="twitter:app:name:ipad" content="Stack Exchange iOS" />
[value] => Stack Exchange iOS
)
[twitter:app:id:ipad] => Array
(
[html] => <meta name="twitter:app:id:ipad" content="871299723" />
[value] => 871299723
)
[twitter:app:url:ipad] => Array
(
[html] => <meta name="twitter:app:url:ipad" content="se-zaphod://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html" />
[value] => se-zaphod://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html
)
[twitter:app:name:googleplay] => Array
(
[html] => <meta name="twitter:app:name:googleplay" content="Stack Exchange Android">
[value] => Stack Exchange Android
)
[twitter:app:url:googleplay] => Array
(
[html] => <meta name="twitter:app:url:googleplay" content="http://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html">
[value] => http://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html
)
[twitter:app:id:googleplay] => Array
(
[html] => <meta name="twitter:app:id:googleplay" content="com.stackexchange.marvin">
[value] => com.stackexchange.marvin
)
)
[metaProperties] => Array
(
[og:url] => Array
(
[html] => <meta property="og:url" content="https://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html"/>
[value] => https://stackoverflow.com/questions/51939042/php-read-three-lines-of-remote-html
)
[og:site_name] => Array
(
[html] => <meta property="og:site_name" content="Stack Overflow" />
[value] => Stack Overflow
)
)
)
Then to actually use it to achieve your purpose:
How can I efficiently parse dozens of sites using this script? The
sites are all ascending numerically. index.php?id=103 index.php?id=104
index.php?id=105
you need to :
-first create an array containing your urls
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>HTML Table</h2>
<table>
<tr>
<th >Id</th>
<th >Title</th>
<th >start_date</th>
<th>end_date</th>
</tr>
<?php
$urls=array(103=>'index.php?id=103',104=> 'index.php?id=104', 105=>'index.php?id=105');
-then loop through this array :
foreach($urls as $id=>$url):
-each iteration you use the function getUrlData() as shown:
$result=getUrlData($url, true);
-then you retrieve the needed information using eg:
?><tr>
<td><?php echo $id; ?></td>
<td><?php echo $result['title']; ?></td>
<td><?php echo $result['metaProperties']['start_date']['value']; ?></td>
<td><?php echo $result['metaProperties']['end_date']['value']; ?></td>
</tr>
to build each line and row.
At the end of the process you would have get your expected table:
Endforeach;?>
</table></body>
</html>

Well, you could solve your issue with the DomDocument class.
$doc = new \DomDocument();
$title = $start = $end = '';
if ($doc->loadHTMLFile($url)) {
// Get the title
$titles = $dom->getElementsByTagName('title');
if ($titles->length > 0) {
$title = $titles->item(0)->nodeValue;
}
// get meta elements
$xpath = new \DOMXPath($doc);
$ends = $xpath->query('//meta[#property="end_date"]');
$if ($ends->length > 0) {
$end = $ends->item(0)->getAttribute('content');
}
$starts = $xpath->query('//meta[#property="start_date"]');
if ($starts->length > 0) {
$start = $starts->item(0)->getAttribute('content');
}
var_dump($title, $start, $end);
}
With the getElementsByTagName method of the DomDocument class you can find the title element in the whole html of a given url. With the DOMXPath class you can retrieve the specific meta data you want. You don 't need much code for finding specific informations in a html string.
The code shown above is not tested.

Related

CURLOPT_RETURNTRANSFER returns HTML in string

I'm trying to parse HTML using CURL DOMDocument or Xpath, but the CURLOPT_RETURNTRANSFER always returns the url's HTML in string which makes it invalid HTML to be parsed
Returned output:
string(102736) "<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/QAPage" class="html__responsive">
<head>
<title>html - PHP outputting text WITHOUT echo/print? - Stack Overflow</title>
<link rel="shortcut icon" href="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
<link rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
<link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">"
PHP snipe see the output
$cc = $http->get($url);
var_dump($cc);
CURL library used: https://github.com/seikan/HTTP/blob/master/class.HTTP.php
When I remove CURLOPT_RETURNTRANSFER I see the HTML without the string(102736), but it echo the url even if i didn't request (reference: curl_exec printing results when I don't want to)
Here is the PHP snipe I used to parse html:
$cc = $http->get($url);
$doc = new \DOMDocument();
$doc->loadHTML($cc);
// all links in document
$links = [];
$arr = $doc->getElementsByTagName("a"); // DOMNodeList Object
foreach($arr as $item) { // DOMElement Object
$href = $item->getAttribute("href");
$text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
$links[] = [
'href' => $href,
'text' => $text
];
}
Any idea?
Check the return value -
print_r($cc);
you will probably find that the output is an array (if the code ran successfully). From the library source, the return of get() is...
return [
'header' => $headers,
'body' => substr($response, $size),
];
So you will need to change the load line to be...
$doc->loadHTML($cc['body']);
Update:
as an example of the above and using this question as the page to work with...
$cc = $http->get("https://stackoverflow.com/questions/51319473/curlopt-returntransfer-returns-html-in-string/51319585?noredirect=1#comment89619183_51319585");
$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($cc['body']);
// all links in document
$links = [];
$arr = $doc->getElementsByTagName("a"); // DOMNodeList Object
foreach($arr as $item) { // DOMElement Object
$href = $item->getAttribute("href");
$text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
$links[] = [
'href' => $href,
'text' => $text
];
}
print_r($links);
Outputs...
Array
(
[0] => Array
(
[href] => #
[text] =>
)
[1] => Array
(
[href] => https://stackoverflow.com
[text] => Stack Overflow
)
[2] => Array
(
[href] => #
[text] =>
)
[3] => Array
(
[href] => https://stackexchange.com/users/?tab=inbox
...

Parsing html using php to an array

I have the below html
<p>text1</p>
<ul>
<li>list-a1</li>
<li>list-a2</li>
<li>list-a3</li>
</ul>
<p>text2</p>
<ul>
<li>list-b1</li>
<li>list-b2</li>
<li>list-b3</li>
</ul>
<p>text3</p>
Does anyone have an idea to parse this html file with php to get this output using complex array
fist one for the tags "p"
and the second for tags "ul" because after above every "p" tag a tag "ul"
Array
(
[0] => Array
(
[value] => text1
(
[il] => list-a1
[il] => list-a2
[il] => list-a3
)
)
[1] => Array
(
[value] => text2
(
[il] => list-b1
[il] => list-b2
[il] => list-b3
)
)
)
I can't use replace or removing all tags cause I use
foreach ($doc->getElementsByTagName('p') as $link)
{
$dont = $link->textContent;
if (strpos($dont, 'document.') === false) {
$links2[] = array(
'value' => $link->textContent, );
}
$er=0;
foreach ($doc->getElementsByTagName('ul') as $link)
{
$dont2 = $link->nodeValue;
//echo $dont2;
if (strpos($dont2, 'favorisContribuer') === false) {
$links3[]= array(
'il' => $link->nodeValue, );
}
You could use the DOMDocument class (http://php.net/manual/en/class.domdocument.php)
You can see an example below.
<?php
$html = '
<p>text1</p>
<ul>
<li>list-a1</li>
<li>list-a2</li>
<li>list-a3</li>
</ul>
<p>text2</p>
<ul>
<li>list-b1</li>
<li>list-b2</li>
<li>list-b3</li>
</ul>
<p>text3</p>
';
$doc = new DOMDocument();
$doc->loadHTML($html);
$textContent = $doc->textContent;
$textContent = trim(preg_replace('/\t+/', '<br>', $textContent));
echo '
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
' . $textContent . '
</body>
</html>
';
?>
However, I would suggest using javascript to find the content and send it to php instead.

PHP $_GET Trouble

So I am working on a website based off of something called the Hypixel API.
When I use a forum where a user can input a name, I get a response of
Player == nullArray ( [0] => Array ( [success] => [cause] => [status] => 204 ) [1] => Array ( [success] => [cause] => [status] => 204 ) )
This is my code:
<?php
$ign = $_GET['ign'];
include_once('HypixelPHP.php');
$HypixelPHP = new HypixelPHP\HypixelPHP(['api_key' => 'BLOCKED']);
// get a player object using the hypixel api object
$player = $HypixelPHP->getPlayer([\HypixelPHP\KEYS::PLAYER_BY_NAME => '$ign']);
if ($player != null) {
echo 'View stats for ' . $player->getFormattedName(true, true);
echo '<br>';
echo '<h1>GAME STATS</h1>';
echo '<h3>Mega Walls</h3>';
echo 'Wins: ' . $player->getStats()->getGameFromID(\HypixelPHP\GameTypes::WALLS3)->getInt('wins');
echo '<br>';
echo 'Kills: ' . $player->getStats()->getGameFromID(\HypixelPHP\GameTypes::WALLS3)->getInt('kills');
echo '<br>';
echo 'Final Kills: ' . $player->getStats()->getGameFromID(\HypixelPHP\GameTypes::WALLS3)->getInt('final');
echo '<br>';
} else {
echo 'Player == null';
print_r($HypixelPHP->getUrlErrors());
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
</html>
In the form, I did do the name thing correct.
This should be the output:
Along with some other stuff.
If you guys can help, thank you!
The issue is that you are passing the single quoted string '$ign'. This ignored the variable you have set for $ign.
http://php.net/manual/en/language.types.string.php

Foreach where first four characters are the same

I have a dir with files like:
0000_01.jpg
0000_02.jpg
0000_03.jpg
2000_01.jpg
2000_02.jpg
2800_01.jpg
3200_01.jpg
And I want to get a table showing me which numbers are available for each article (0000, 2000, 2800, 3200 and so on).
I got this:
$files = glob('new/' . $map . '*.{jpg}', GLOB_BRACE);
foreach($files as $file) {
}
Now I need to check if the four characters are matching and check is _01, _02 and _03 are available or not.
I know about the php function substr() but I just can't figure out how to get that implemented in the foreach or something..
I want to get a table showing me like this:
artnr _01 _02 _03
0000 x x x
2000 x x
2800 x
..
Can anyone point me in the right direction of doing this?
PHP code
$files = array(
"0000_01.jpg",
"0000_02.jpg",
"0000_03.jpg",
"2000_01.jpg",
"2000_02.jpg",
"2800_01.jpg",
"3200_01.jpg",
);
$arr = array();
foreach ($files as $file) {
$arr[substr($file, 0, 4)][substr($file,5, 2)] = true;
}
and you can check the existance of '01' in '0000' by :
if ($arr["0000"]["01"])
Result
var_export($arr) :
array (
'0000' =>
array (
'01' => true,
'02' => true,
'03' => true,
),
2000 =>
array (
'01' => true,
'02' => true,
),
2800 =>
array (
'01' => true,
),
3200 =>
array (
'01' => true,
),
)
Creating the table
<table>
<tr>
<td width="80">artnr</td>
<td width="80">_01</td>
<td width="80">_02</td>
<td width="80">_03</td>
</tr>
<?php foreach ($arr as $key => $value) { ?>
<tr>
<td><?php echo $key ?></td>
<td><?php echo $value['01'] ? "X" : "" ?></td>
<td><?php echo $value['02'] ? "X" : "" ?></td>
<td><?php echo $value['03'] ? "X" : "" ?></td>
</tr>
<?php } ?>
</table>
Result
If I understood your question correctly, then you might use array_column() and array_unique() to get column titles and row headers:
<?php
header('Content-Type: text/plain; charset=utf-8');
$files = [
"0000_01.jpg",
"0000_02.jpg",
"0000_03.jpg",
"2000_01.jpg",
"2000_02.jpg",
"2800_01.jpg",
"3200_01.jpg",
];
$buffer = array_map(
function($name){ return explode('_', $name); },
$files
);
$groups = array_unique(array_column($buffer, 0));
$columns = array_unique(array_column($buffer, 1));
print_r($groups);
print_r($columns);
?>
Shows:
Array
(
[0] => 0000
[3] => 2000
[5] => 2800
[6] => 3200
)
Array
(
[0] => 01.jpg
[1] => 02.jpg
[2] => 03.jpg
)
Then build their intersection:
foreach($groups as $group){
foreach($columns as $column){
if(in_array("{$group}_{$column}", $files)){
echo "{$group}_{$column}", ' <- exists', PHP_EOL;
} else {
echo "{$group}_{$column}", ' <- not exists', PHP_EOL;
}
}
}
Shows:
0000_01.jpg <- exists
0000_02.jpg <- exists
0000_03.jpg <- exists
2000_01.jpg <- exists
2000_02.jpg <- exists
2000_03.jpg <- not exists
2800_01.jpg <- exists
2800_02.jpg <- not exists
2800_03.jpg <- not exists
3200_01.jpg <- exists
3200_02.jpg <- not exists
3200_03.jpg <- not exists
Another solution:
$a=array(
'0000_01.jpg',
'0000_02.jpg',
'0000_03.jpg',
'2000_01.jpg',
'2000_02.jpg',
'2800_01.jpg',
'3200_01.jpg');
$result_array=array();
foreach ($a as $item) {
list($art, $artnr) = array_slice(str_split($item, 4),0,2);
$result_array[$art][] = substr($artnr,1,-1);
}
print '<pre>';
print_r($result_array);
print '</pre>';
Output:
Array
(
[0000] => Array
(
[0] => 01
[1] => 02
[2] => 03
)
[2000] => Array
(
[0] => 01
[1] => 02
)
[2800] => Array
(
[0] => 01
)
[3200] => Array
(
[0] => 01
)
)
Related question: Grouping and resorting values in PHP array using substrings
how about this:
<?php
$files = array('0000_01.jpg', '0000_02.jpg', '0000_03.jpg', '2000_01.jpg', '2000_02.jpg', '2800_01.jpg', '3200_01.jpg');
?>
<!DOCTYPE html>
<html>
<head>
<title>testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<?php
$list = array();
foreach ($files as $file) {
preg_match('/(\d+).?(\d+)\./', $file, $match);
$name = $match[1];
$col = $match[2];
$list[$col][] = $name;
}
foreach ($list as $key => $item) {
foreach ($item as $value) {
echo "$key, $value<br/>";
}
}
?>
</body>
</html>
<?php
$files = glob('new/' . $map . '*.{jpg}', GLOB_BRACE);
$tableArr = array();
foreach($files as $file) {
$fileNameArr = explode('_', $file) {
if(count($fileNameArr) == 2 AND strlen($fileNameArr[0]) == 4 AND strlen($fileNameArr[1]) == 2) {
$tableArr[$fileNameArr[0]][$fileNameArr[1]] = 'x';
}
}
}
?>
<table>
<thead>
<tr>
<th>
artnr
</th>
<?php
foreach(array_keys($fileNameArr) as $key) {
?>
<th><?=$key?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($fileNameArr as $key=>$row) {
?>
<tr>
<td><?=$key?></td>
<?php
foreach($row as $col) {
?>
<td><?=$col?></td>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
Why not a regular expression?
for($i=1;i<=3;i++){
$result = preg_match("/_0"+$i+"/",$file);
}
preg_match on PHP.net

Meta tag helper with Open Graph tags

How do I use the Codeigniter Metatag helper with the Facebook open graph metatags?
the metatag helper wants to inject name as the first attribute which Facebook doesn't use. Is there any way to use the helper to add my own custom scheme? It seems there are some defaults like 'name' that i can't get rid of using the helper.
what i did:
$meta = array(
array('property' => 'og:type', 'content' => 'movie'),
array('property' => 'og:video:height', 'content' => '260'),
array('property' => 'og:video:width', 'content' => '420'),
array('property' => 'og:video:type', 'content' => 'application/x-shockwave-flash'),
array('property' => 'og:title', 'content' => $data['video']),
array('property' => 'og:description', 'content' => $data['video_desc']),
array('property' => 'og:image', 'content' => 'http://i2.ytimg.com/vi/'.$data['links']['extra_link_info']['original_key'].'/hqdefault.jpg'),
array('property' => 'og:video', 'content' => 'https://www.youtube.com/v/'.$data['links']['extra_link_info']['original_key'].'?version=3&autohide=1&autoplay=1')
);
what i get:
<meta name="" content="movie" />
<meta name="" content="260" />
<meta name="" content="420" />
<meta name="" content="application/x-shockwave-flash" />
<meta name="" content="xxxxxxxx" />
<meta name="" content="xxxxxxxx" />
<meta name="" content="http://i2.ytimg.com/vi/xxxxxxxx/hqdefault.jpg" />
<meta name="" content="xxxxxxxx" />
what i want:
<meta property="og:tyoe" content="movie" />
<meta property="og:video:height" content="260" />
<meta property="og:video:width" content="420" />
<meta property="og:video:type" content="application/x-shockwave-flash" />
<meta property="og:title" content="xxxxxxxx" />
<meta property="og:description" content="xxxxxxxx" />
<meta property="og:image" content="http://i2.ytimg.com/vi/xxxxxxxx/hqdefault.jpg" />
<meta property="og:video" content="xxxxxxxx" />
I used this
MY_html_helper.php
if (!function_exists('meta')) {
function meta($name = '', $content = '', $type = 'name', $newline = "\n") {
// Since we allow the data to be passes as a string, a simple array
// or a multidimensional one, we need to do a little prepping.
if (!is_array($name)) {
$name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
} else {
// Turn single array into multidimensional
if (isset($name['name'])) {
$name = array($name);
}
}
$str = '';
foreach ($name as $meta) {
if ((!isset($meta['type']) OR $meta['type'] == 'name')) {
$type = 'name';
} else if ($meta['type'] == 'equiv') {
$type = 'http-equiv';
} else {
$type = $meta['type'];
}
$name = (!isset($meta['name'])) ? '' : $meta['name'];
$content = (!isset($meta['content'])) ? '' : $meta['content'];
$newline = (!isset($meta['newline'])) ? "\n" : $meta['newline'];
$str .= '<meta ' . $type . '="' . $name . '" content="' . $content . '" />' . $newline;
}
return $str;
}
}
Usage :
$meta = array(
array('name' => 'og:title', 'content' => 'my great title', 'type' => 'property')
);
Fixed it:
Extended the HTML helper and changed all the name references to property.
MY_html_helper
<?php
/**
* Generates meta tags from an array of key/values
*
* #access public
* #param array
* #return string
*/
if ( ! function_exists('meta'))
{
function meta($property = '', $content = '', $type = 'property', $newline = "\n")
{
// Since we allow the data to be passes as a string, a simple array
// or a multidimensional one, we need to do a little prepping.
if ( ! is_array($property))
{
$property = array(array('property' => $property, 'content' => $content, 'type' => $type, 'newline' => $newline));
}
else
{
// Turn single array into multidimensional
if (isset($property['property']))
{
$property = array($property);
}
}
$str = '';
foreach ($property as $meta)
{
$type = ( ! isset($meta['type']) OR $meta['type'] == 'property') ? 'property' : 'http-equiv';
$property = ( ! isset($meta['property'])) ? '' : $meta['property'];
$content = ( ! isset($meta['content'])) ? '' : $meta['content'];
$newline = ( ! isset($meta['newline'])) ? "\n" : $meta['newline'];
$str .= '<meta '.$type.'="'.$property.'" content="'.$content.'" />'.$newline;
}
return $str;
}
}

Categories