yahoo weather Api - php

I would like to get the temperature value from Yahoo's weather API. I have found a tutorial but in the tutorial he is getting a different value. Could some one help me modify the tutorial that it could get the temp value from Yahoo's weather RSS feed?
<yweather:condition text="Partly Cloudy" code="30" temp="3"
date="Mon, 09 Apr 2012 3:48 pm EEST" />
RSS feed: http://weather.yahooapis.com/forecastrss?w=566473&u=c
The tutorial I followed: http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/
If some one has a better solution for getting the value don't hesitate to say it. :)

This seems pretty straightforward. From the tutorial:
Since the only bit of information we care about is the yweather:condition element's text attribute, we're going to avoid creating an XML parsing object and use a short regular expression.
So, just look at the line with the regular expression:
$weather_class = format_result(
get_match( '/<yweather:condition text="(.*)"/isU', $data )
);
This is actually a bad regular expression because it assumes text will always be the first attribute (and that there'll always be that weird double-space. Here's a regular expression that will get the temp attribute regardless of where it falls:
/<yweather:condition\s+(?:.*\s)?temp="(.+)"/isU
Substitute that for the regular expression given to get_match() and you should be good to go.
Oh, and lest I be kicked off SO for not saying so: Attempting to parse arbitrary HTML XML with regular expressions is the path to madness.

Related

RegEx for string replacement in MySQL Database

I have recently upgrade an IPB version of my forum but the quotes were not upgraded and IPS is not giving the support I need.
I need to build a regular expression do find and replace
For example this is the old forum format:
<div class="quotetop">QUOTE(Cleber__v # Apr 14 2015, 12:25 PM)
<a href="index.php?act=findpost&pid=2778161"><{POST_SNAPBACK}>
</a></div><div class="quotemain"><!--quotec-->
TEXT TO BE KEPT
<!--QuoteEnd-->
</div><!--QuoteEEnd-->
And this is the new format it should be on:
`<div>
<blockquote class="ipsQuote"
data-cite="Em 14/04/2015, (Cleber__v disse:" data-ipsquote=""
data-ipsquote-timestamp="1428004301" data-ipsquote-userid="2350"
data-ipsquote-username="Cleber__v" data-ipsquote-contapp="forums"
data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic"
data-ipsquote-contentid="105179" data-ipsquote-contentcommentid="2768819">
TEXT TO BE INSERTED</blockquote></div><p><span>​</span></p>`
So I need to find the content, save Username, postID (or commentid), time and text and replace it with the correct format.
I've been researching regex for about a week now with no sucess on how to make this happen
Anybody could help? Thank you
You should try for youself and give us the regex you tried, but here is a first step for you :
.*?>QUOTE\((?P<name>.*)\ \#\ (?P<date>[^\)]+).*?pid\=(?P<pid>[0-9]*).*?\<\!\-\-quotec\-\-\>(?P<text>.*?)\<\!\-\-QuoteEnd\-\-\>
See here how it works : https://regex101.com/r/dM0eG3/1 and how the match information corresponds to your need.
NB : you must remove all new line characters before applying this regex, but this is fairly easy to do in PHP or in any language you might use to create your db upgrade script.
That will extract all the relevant information from your text. Replacing these in the new format is left as an exercise for the reader.

XML Youtube, access to yt:accessControl? the character :

I have
this xml file
and i'm trying to access to the value of attribute permission in the tag yt:accessControl in php
echo (string)$xmlyt->entry->children('yt')->{'accessControl'}->attributes()->$actionAttr."------------";
but i have the error
Node no longer exists
Understanding how SimpleXML works and XML generally is greatly beneficial to doing this...
You can discover things through trial and error and end up with something like this:
$sxml=simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$videoID.'?v=2');
$yt = $sxml->children('http://gdata.youtube.com/schemas/2007');
print_r($yt->accessControl->attributes());
print_r($yt->accessControl[4]->attributes());
For instance, this will give you permissions for the first and fifth actions, which happen to be comment and embed ATM (should probably loop through all to identify the ones you're interested instead of relying on the order).
Hope this helps,
aL

Search through multiple hosted XML files for a string

I'm working on a website that uses a lot of XML-files as data (150 in total and probably growing). Each page is an XML-file.
What I'm looking for is a way to look for a string through the XML-files. I'm not sure what programming language to use for this XML search engine.
I'm familiar with PHP, JavaScript, JQuery. So I'd prefer using those languages.
Thanks a bunch!
UPDATE: I'm looking for a solution that works quickly.
Ideally, the function returns the tagname that contains the searchstring.
If, for instance, the XML is as follows:
<article-1>This is a great story.</article-1>
If one would search for 'story', it would return 'article-1'.
I'm not quite sure on how to do this with a regular expression.
PHP can do this. Here's an example:
foreach(glob("{foldera/*.xml,folderb/*.xml}",GLOB_BRACE) as $filename) {
$xml = simplexml_load_file($filename);
//use regular expressions to find your string
}
You simply iterate through each file on your server using glob() with a foreach loop.
Sounds like a problem that could be solved with grep and regular expressions. Without knowing what string you're looking for it's not possible to say exactly what you should do, but reading some documentation on grep should get you started down the right path.

PHP Simple HTML DOM Parser denies to handle [invalid] HTML - first trial fails

g day dear community - hello all!
well I am trying to select either a class or an id using PHP Simple HTML DOM Parser with absolutely no luck. Perhaps i have to study the manpages again and again.
Well - the DOM-technique somewhat goes over my head:
But my example is very simple and seems to comply to the examples given in the manual (simplehtmldom.sourceforge AT net/manual.htm) but it just wont work, it's driving me up the wall. Other example scripts given with simple dom work fine.
See the example: http://www.aktive-buergerschaft.de/buergerstiftungsfinder
This is the easiest example i have found ... The question is - how to parse it?
Should i do it with Perl - The example HTML page is invalid HTML.
I do not know if the Simple HTML DOM Parser is able to handle badly malformed HTML
(probably not). And then i am lost.
Well: it is pretty hard to believe - but you can get the content with file_get_contents: But you afterwards have to do the parser job! And there i have some missing parts!
Finally: if i cannot get it to run i can try out some Perl parsers eg HTML::TreeBuilder::XPath
1: check whether file_get_contents is working!!!!
2: If no use curl or fopen or telnet to read the data.
Simple Html Dom filters all the noise can process malformed tags also...
Problem might be with your data retrieving

What is the fastest way to convert html table to php array?

are there build in functions in latest versions of php specially designed to aid in this task ?
Use a DOM parser like SimpleXML to split the HTML code into nodes, and walk through the nodes to build the array.
For broken/invalid HTML, SimpleHTMLDOM is more lenient (but it's not built in).
String replace and explode would work if the HTML code is clean and always the same, as soon as you have new attributes it will brake.
So only dependable solution would be using regular expressions or XML/HTML parser.
Check http://php.net/manual/en/book.dom.php
An alternative to using a native DOM parser could be using YQL. This way you dont have to do the actual parsing yourself. The YQL Web Service enables applications to query, filter, and combine data from different sources across the Internet.
For instance, to grab the HTML table with the class example given at
http://www.w3schools.com/html/html_tables.asp
you can do
$yql = 'http://tinyurl.com/yql-table-grab';
$yql = json_decode(file_get_contents($yql));
print_r( $yql->query->results );
I've deliberated shortened the URL so it does not mess up the answer. $yql actually links to the YQL API, adds some options and contains the query:
select * from html
where xpath="//table[#class='example']"
and url="http://www.w3schools.com/html/html_tables.asp"
YQL can return JSON and XML. I've made it return JSON and decoded this then, which then results in a nested structure of stdClass objects and Arrays (so it's not all arrays). You have to see if that fits your needs.
You try out the interactive YQL console to see how it works.
i dont know if this is the faster , but you can check this class (using preg_replace)
http://wonshik.com/snippet/Convert-HTML-Table-into-a-PHP-Array
If you want to convert the html-description of a table, here's how I would do it:
remove all closing tags (</...>) ( http://php.net/manual/de/function.str-replace.php)
split string at opening tags (<...>) using a regular expression ( http://php.net/manual/en/function.split.php)
You have to work out the details on your own, since I do not know if you want to handle different lines as subarrays or you want to merge all lines into one big array or something else.
you could use the explode-function to turn the table cols and rows into arrays.
see: php explode

Categories