I want to extract title and image for each item mentioned in http://rss.cnn.com/rss/edition.rss feed using PHP ,could anyone please help me out ?
Thanx
you can try simplexml_load_string():
Exemple:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
$xml = simplexml_load_string($string);
var_dump($xml);
?>
Related
Can someone please help me with what I am doing wrong here. Quite a novice when it comes to PHP
Here is my code:
<?php echo $breadcrumb['text']; str_replace("<br />",""); ?>
I think the correct way of executing that is this.:
echo str_replace("<br />", "", $breadcrumb['text']);
In this case, you want to replace all br-tags with "" in the $breadcrumb['text'] variable.
Look at the documentation for that function here: http://php.net/manual/en/function.str-replace.php
If I understand:
<?php
$breadcrumb['text'] = "Something <br /> Next line but now on first <br />";
str_replace("<br />","", $breadcrumb['text']);
echo $bread;
?>
I want to use jQueryUI Autocomplete on my Drupal site and I downloaded the .js files that are needed for it to function. Now after that, I saved my .js files on my themes folder located at /sites/all/themes/advanced/js. And in my page.tpl.php there's this code,
<head>
<?php print $head ?>
<title><?php print $head_title ?></title>
<?php print $styles ?>
<?php print $scripts ?>
<?php print phptemplate_get_scripts(); ?>
<?php if ($user->uid) print phptemplate_get_scripts_advanced(); ?>
<!--[if lt IE 7]>
<?php print phptemplate_get_ie_styles(); ?>
<![endif]-->
</head>
with the code above I'm assuming that my .js files will be included but when I reloaded the page and check the running scripts through Firebug, I could not find them. What did I miss? Thanks in advance.
Got it. I did try to put my scripts manually on my template.php and that solved it. :)
If I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<response>
<status>SUCCESS</status>
<result>
<message>
<field>
<name>referenceNumber</name>
<type>string</type>
<required>true</required>
<max>8</max>
<min>0</min>
<decimal_places>0</decimal_places>
</field> ...
Is there a way to get the value for response/result/message? I know I can use a find("message") but if there are more than 1 message node then it doesn't work. I would like to know if there is a way to get to a particular node without 'guessing' if you know what I mean.
Thanks
you want to use find().each('message)` http://api.jquery.com/jQuery.each/ kinda like
`$(xml).find("listing").each(function (){
// Do stuff.
});
I have 2 tables, page and settings.
page is just a bunch of fields, such as name and slug, and has 3 other fields for meta tags (title, keywords, description) and displays a cms page.
The settings has 3 fields: default_meta_title, default_meta_keywords, default_meta_description
Now what I'm looking to do is to display the default_meta_* tags in the HTML source if the page I am on does not have the particular meta info set from the cms page.
All pages, except the homepage is managed this way, so I was thinking I'd need to add some code to the layout.php to get this to work.
So the homepage will display my default_meta_*, as I cannot set this in the cms pages table.
There are two ways to solve the problem.
First is to use sfYaml class to update view.yml with default meta tags (see documentation about view.yml). After that if specific page should use another metas you can override defaults with addMeta method of response object
Second (as ManseUK suggested) is to declare slot placing code like this into layout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php include_javascripts() ?>
<?php include_stylesheets() ?>
<?php include_title() ?>
<?php if (has_slot('metas')): ?>
<?php include_slot('metas') ?>
<?php else: ?>
<?php include_component('page', 'metas') ?>
<?php endif; ?>
<link rel="shortcut icon" href="/favicon.ico" />
</head>
<body>
Default metas will be rendered via page components. On top of your template (i guess modules/page/templates/showSuccess.php) place code
<?php slot('metas') ?>
<?php if($page->hasMetas()):?>
<!-- code to render nondefault page metas -->
<?php echo $page->getMetas(); ?>
<?php else: ?>
<?php include_component('page', 'metas') ?>
<?php endif;?>
<?php end_slot() ?>
I assume that you will replace $page->hasMetas() with real code that will check if your page object has metatags.
Actually i would prefer to go further and code page components to accept parameters. Code in a template will look like
<?php slot('metas') ?>
<?php include_component('page', 'metas', array('metas'=>$page->getMetas())) ?>
<?php end_slot() ?>
Deciding which metas (default or not) should be rendered will take place in page components (i assume that you can easily retrieve defaul;t settings from your database). If no parameters were passed (see layout code) than your component should also render default metas.
I hope this will help.
You could use a slot - check for existence of the slot in the layout - if it exists then add the custom meta fields - if not add the default ones
Alright, so the ultimate goal here is to parse the data inside of an xml response. The response comes in the format of a ruby string. The problem is that I'm getting an error when creating the xml file from that string (I know for a fact that response.body.to_s is a valid string of xml:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<CardTxn>
<authcode>123</authcode>
<card_scheme>Mastercard</card_scheme>
<country>United Kingdom</country>
</CardTxn>
<datacash_reference>XXXX</datacash_reference>
<merchantreference>XX0001</merchantreference>
<mode>TEST</mode>
<reason>ACCEPTED</reason>
<status>1</status>
<time>1286477267</time>
</Response>
Inside the ruby method I try to generate an xml file:
doc = Nokogiri::XML(response.body.to_s)
the output of doc.to_s after the above code executes is:
<?xml version="1.0"?>
Any ideas why the file is not getting generated correctly?
This works for me on 1.9.2. Notice it's Nokogiri::XML.parse().
require 'nokogiri'
asdf = %q{<?xml version="1.0" encoding="UTF-8"?>
<Response>
<CardTxn>
<authcode>123</authcode>
<card_scheme>Mastercard</card_scheme>
<country>United Kingdom</country>
</CardTxn>
<datacash_reference>XXXX</datacash_reference>
<merchantreference>XX0001</merchantreference>
<mode>TEST</mode>
<reason>ACCEPTED</reason>
<status>1</status>
<time>1286477267</time>
</Response>
}
doc = Nokogiri::XML.parse(asdf)
print doc.to_s
This parses the XML into a Nokogiri XML document, but doesn't create a file. doc.to_s only shows you what it would be like if you printed it.
To create a file replace "print doc.to_s" with
File.open('xml.out', 'w') do |fo|
fo.print doc.to_s
end