JSON output in array using PHP explode - php

So, I have an output from an API using JSON:
{"listings":[{"adoption_fee":"$200","adoption_process":"For cats, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-cats/\"\u003ePre-Adoption Questionnaire - Cats\u003c/a\u003e.\r\n\r\nFor dogs, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-dogs/\"\u003ePre-Adoption Questionnaire - Dogs\u003c/a\u003e.\r\n\r\nFor more information on our Adoption Process, please visit this \u003ca href=\"http://www.haart.org.au/our-adoption-process/\"\u003elink\u003c/a\u003e.\r\n\r\nPlease make sure that you are familiar with our \u003ca href=\"http://www.haart.org.au/adoption-agreement/\"\u003eAdoption Agreement\u003c/a\u003e as it has recently changed.\r\n\r\nFor more information on any of our animals, please \u003ca href=\"http://www.haart.org.au/contact-us/\"\u003eContact Us\u003c/a\u003e.","age":"5 years 8 months","breeds":["Domestic Short Hair"],"breeds_display":"Domestic Short Hair","coat":"Short","contact_name":null,"contact_number":"08 6336 9410","contact_preferred_method":"Email","created_at":"2/9/2014 15:23","date_of_birth":"12/7/2012","desexed":true,"foster_needed":false,"gender":"Female","group":"Homeless and Abused Animal Rescue Team","heart_worm_treated":null,"id":316602,"interstate":false,"last_updated":"22/3/2018 9:40","medical_notes":"","microchip_number":"","mix":false,"multiple_animals":false,"name":"Whinney HC13-154","personality":"Whinney is an independent girl who likes lazing around the house, she's not bothered by other cats or dogs as long as they don't want to cuddle too much, then she will find her own alone space. \r\n\r\nShe will come up for the occasional cuddle but generally a cosy spot at the end of the bed or couch is all this beautiful girl craves :) \r\n\r\n** PLEASE NOTE: all HAART cats are to be adopted as indoor only cats for their safety and to comply with the legal requirements of the Cat Act. \r\nHAART recommends the use of Oscillot cat fencing or feline safe Catio's/portable caboodle to ensure they have access to the outdoors. \r\nPlease ask us for information on other suitable products **","photos":[{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg"}],"senior":false,"size":null,"species":"Cat","state":"WA","postcode":"6000","vaccinated":"Yes","wormed":"Yes"}
I want to grab the "adoption_process" and "personality" parts and using PHP break the lines when "\n" is in the output.
I have the following:
$adoption_process = {
foreach ($line in explode("\n", $json['adoption_process']))
{$line = trim($line);
}
}
Then I'm using print_r to echo the output. But it's not working.
Open to suggestions on other (better) ways to do this.

The JSON you mention is invalid. It's not properly closed array at the end. I have copy the JSON and put changes. Just have look below, Hope it will help you out.
<?php
$json = '{"listings":[{"adoption_fee":"$200","adoption_process":"For cats, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-cats/\"\u003ePre-Adoption Questionnaire - Cats\u003c/a\u003e.\r\n\r\nFor dogs, please fill out our \u003ca href=\"http://www.haart.org.au/pre-adoption-form-dogs/\"\u003ePre-Adoption Questionnaire - Dogs\u003c/a\u003e.\r\n\r\nFor more information on our Adoption Process, please visit this \u003ca href=\"http://www.haart.org.au/our-adoption-process/\"\u003elink\u003c/a\u003e.\r\n\r\nPlease make sure that you are familiar with our \u003ca href=\"http://www.haart.org.au/adoption-agreement/\"\u003eAdoption Agreement\u003c/a\u003e as it has recently changed.\r\n\r\nFor more information on any of our animals, please \u003ca href=\"http://www.haart.org.au/contact-us/\"\u003eContact Us\u003c/a\u003e.","age":"5 years 8 months","breeds":["Domestic Short Hair"],"breeds_display":"Domestic Short Hair","coat":"Short","contact_name":null,"contact_number":"08 6336 9410","contact_preferred_method":"Email","created_at":"2/9/2014 15:23","date_of_birth":"12/7/2012","desexed":true,"foster_needed":false,"gender":"Female","group":"Homeless and Abused Animal Rescue Team","heart_worm_treated":null,"id":316602,"interstate":false,"last_updated":"22/3/2018 9:40","medical_notes":"","microchip_number":"","mix":false,"multiple_animals":false,"name":"Whinney HC13-154","personality":"Whinney is an independent girl who likes lazing around the house, she\'s not bothered by other cats or dogs as long as they don\'t want to cuddle too much, then she will find her own alone space. \r\n\r\nShe will come up for the occasional cuddle but generally a cosy spot at the end of the bed or couch is all this beautiful girl craves :) \r\n\r\n** PLEASE NOTE: all HAART cats are to be adopted as indoor only cats for their safety and to comply with the legal requirements of the Cat Act. \r\nHAART recommends the use of Oscillot cat fencing or feline safe Catio\'s/portable caboodle to ensure they have access to the outdoors. \r\nPlease ask us for information on other suitable products **","photos":[{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_db5ee_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_fb958_orig.jpg"},{"small_80":"https://res.cloudinary.com/petrescue/image/upload/h_80,w_80,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","medium_130":"https://res.cloudinary.com/petrescue/image/upload/h_130,w_130,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","large_340":"https://res.cloudinary.com/petrescue/image/upload/h_340,w_340,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg","xlarge_900":"https://res.cloudinary.com/petrescue/image/upload/h_900,w_900,c_pad,q_auto:best/petrescue-production-s3/uploads/pet_photos/2014/9/2/316602_9030a_orig.jpg"}],"senior":false,"size":null,"species":"Cat","state":"WA","postcode":"6000","vaccinated":"Yes","wormed":"Yes"}]}';
$data = json_decode($json,true);
foreach($data['listings'] as $key => $value){
$process = explode("\n",$value['adoption_process']);
foreach($process as $k => $v){
if(trim($v)){
$line[] = $v;
}
}
}
print_r($line);

Related

php search text file for any wav file names

I have a series of files which contain raw text or json data, in these files will be wav file names. All of the wav file have the suffix of .wav
Is there anyway using php I can search an individual text or json file and return an array of of any .wav files found ?
This example of random text contains 6 .wav files, how would I search this and extract the filenames ?
Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed playme.wav principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if.
Spot of come to ever test.wav hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated welcome.wav especially. Unfeeling one provision are smallness resembled repulsive.
Raising say express had chiefly detract demands she. Quiet led own cause three him. Front no party young abode state up. Saved he do fruit woody of to. Met defective are allowance two perceived listening consulted contained. It chicken oh colonel pressed excited suppose to shortly. He improve started no we manners another.wav however effects. Prospect humoured mistress to by proposal marianne attended. Simplicity the far admiration preference everything. Up help home head spot an he room in.
Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity. Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me last.wav contained he pronounce do. Excellence inquietude assistance precaution any impression man sufficient.
I've tries this, but I get no results.
$lines = file('test.txt');
foreach ($lines as $line_num => $line) {
$line = trim($line);
if (strpos($line, '*.wav') !== false) {
echo ($line);
}
}
The above text should return :
newfile.wav
playme.wav
test.wav
welcome.wav
another.wav
last.wav
Thanks
UPDATE:
Using the following:
$text = file_get_contents('test.txt');
preg_match_all('/\w+\.wav/', $text, $matches);
var_dump($matches);
results in an array of :
array(1) {
[0]=>
array(6) {
[0]=>
string(11) "newfile.wav"
[1]=>
string(10) "playme.wav"
[2]=>
string(8) "test.wav"
[3]=>
string(11) "welcome.wav"
[4]=>
string(11) "another.wav"
[5]=>
string(8) "last.wav"
}
}
So an array of the wav files with in an array, how do I get just the array of wav files ? Thanks
This does't work correctly for wav files with spaces in there names.
any ideas ?
This tool might help you to design an expression as you wish and test it, maybe something similar to:
([a-z]+\.wav)
You can also add more boundaries to it, if you might want to.
here]2]2
Graph
This graph shows how the expression would work and you can visualize other expressions in this link:
PHP Code
You could also use preg_match_all to do so, maybe something similar to:
$re = '/([a-z]+\.wav)/m';
$str = 'Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed playme.wav principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if.
Spot of come to ever test.wav hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated welcome.wav especially. Unfeeling one provision are smallness resembled repulsive.
Raising say express had chiefly detract demands she. Quiet led own cause three him. Front no party young abode state up. Saved he do fruit woody of to. Met defective are allowance two perceived listening consulted contained. It chicken oh colonel pressed excited suppose to shortly. He improve started no we manners another.wav however effects. Prospect humoured mistress to by proposal marianne attended. Simplicity the far admiration preference everything. Up help home head spot an he room in.
Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity. Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me last.wav contained he pronounce do. Excellence inquietude assistance precaution any impression man sufficient. ';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Test Script for RegEx
const regex = /([a-z]+\.wav)/gm;
const str = `Spoke as as other again ye. Hard on to roof he drew. So sell side newfile.wav ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed playme.wav principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if.
Spot of come to ever test.wav hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behaviour. Him yet devonshire celebrated welcome.wav especially. Unfeeling one provision are smallness resembled repulsive.
Raising say express had chiefly detract demands she. Quiet led own cause three him. Front no party young abode state up. Saved he do fruit woody of to. Met defective are allowance two perceived listening consulted contained. It chicken oh colonel pressed excited suppose to shortly. He improve started no we manners another.wav however effects. Prospect humoured mistress to by proposal marianne attended. Simplicity the far admiration preference everything. Up help home head spot an he room in.
Talent she for lively eat led sister. Entrance strongly packages she out rendered get quitting denoting led. Dwelling confined improved it he no doubtful raptures. Several carried through an of up attempt gravity. Situation to be at offending elsewhere distrusts if. Particular use for considered projection cultivated. Worth of do doubt shall it their. Extensive existence up me last.wav contained he pronounce do. Excellence inquietude assistance precaution any impression man sufficient. `;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
This is why regular expressions were invented.
$text = file_get_contents('test.txt');
preg_match_all('/(\w+\.wav)/', $text, $matches);
var_dump($matches[0]);
Some good resources:
preg_match
preg_replace
regex101.com allows you to test expressions realtime
output:
array(6) {
[0] => string(11) "newfile.wav"
[1] => string(10) "playme.wav"
[2] => string(8) "test.wav"
[3] => string(11) "welcome.wav"
[4] => string(11) "another.wav"
[5] => string(8) "last.wav"
}
You are almost there. You can explode the $line in terms of spaces. Now, you go through each word and check if ends with a .wav extension. If yes, you print the word.
<?php
foreach ($lines as $line_num => $line) {
$line = trim($line);
$words = explode(" ",$line);
foreach($words as $each_word){
$wav_index = strpos($each_word, '.wav');
if ($wav_index !== false && $wav_index === strlen($each_word) - 4) { // strict check to make sure string ends with a .wav and not being elsewhere
echo $each_word,PHP_EOL;
}
}
}

Filtering RSS links with regular expressions

I'm a bit of a noob, but have been getting my feet wet building a site in php on localhost. The problem that I'm having is that I can't figure out how to filter RSS content that contains a regular expression in their links.
My code to display a RSS feed with PHP:
<?php
///// RSS FEED CODE
function getFeed1($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
}
echo "</ul>";
}
getFeed1("http://www.drf.com/feeds/all-articles-of-track/SA");
?>
The results are displayed as such in a browser as links to a page,
Espinoza wins George Woolf Memorial Jockey Award
Dortmund will get month to clear up foot problem
Abrams hopes McHeat stays hot for Sensational Star
Santa Anita attendance up, handle down
Hot Market returns from long absence on hillside turf course
Moon Over Paris, Divina Comedia key to pick six
Millionaire Alert Bay looks to pad bankroll in Sensational Star
Santa Anita to replace turf course this summer
Free: Santa Anita horses to watch for week of Feb. 22
Iron Rob vanned off after winning Baffle Stakes
I am trying to figure out how to use an if-statement that will filter out the links(href) that start with “http://www.drf.com/news/preview/”.
So the results will look like:
Espinoza wins George Woolf Memorial Jockey Award
Santa Anita attendance up, handle down
Millionaire Alert Bay looks to pad bankroll in Sensational Star
Santa Anita to replace turf course this summer
Iron Rob vanned off after winning Baffle Stakes
I've spent the last two days trying different variations of:
if (strpos($x, 'http://www.drf.com/news/preview/') !== false)
and
if (preg_match('http://www.drf.com/news/preview/', $x))
Yet I can't get the syntax right or I'm screwing up somewhere.
I have found post that suggest using third party filters, or the dead yahoo pipes, yet I have a feeling that what I seek can be accomplished with an if-statement. I have yet to find anything that can parse out a rss href using a regular expression.
For the people who know php, what am I missing? I have spent the last two days googling and trying different things mentioned on the internet, but to no avail. I know the chase is always better then the catch, yet I lost the tracks of my prey. Please Help by pointing me, and others who found this post, find the trail.
Thank you
This is the regular expression you are looking for:
/^(http\:\/\/www\.drf\.com\/news\/preview\/)/i
You should accept HTTPS too with a small modification:
/^(https?\:\/\/www\.drf\.com\/news\/preview\/)/i
And do not fall back on the www subdomain!
/^(https?\:\/\/(www\.)?drf\.com\/news\/preview\/)/i

User Friendly, Easy to Remember Coupon Codes

I want to create coupon codes that users can remember easily. My idea is something like:
squirrel45
nantucket23
That is, a real word chosen randomly from a long dictionary list (preferably compiled for this purpose) combined 2 random digits. My questions are:
Where can I find such a dictionary list?
Do you see any problems with the system? (security is not ultra important here, just something reasonable is fine)
Can you suggest any good improvements or alternatives?
Fwiw I am not crazy about the Markov word generators because I think their idiosyncrasies would be too hard to remember. I'd like a client to be able to keep the code in his head, and tell it to the merchant when he arrives to redeem it.
Thanks,
Jonah
Word lists are easy to find. Make sure you sanity filter them for foul words ;)
Here's a huge word list that can be easily scrubbed:
http://www.scrabble-assoc.com/boards/dictionary/10-15-20030401.txt
From there you can easily load in words into your database and create your coupon code like so:
$coupon_code = $rand_word . rand(20,99);
After you do this, simply store your coupon code in the database and whenever you make a new code, check it against existing codes before you apply it. Even slim odds are possible odds.
More word lists in various formats:
http://scrabble.wonderhowto.com/blog/ultimate-scrabble-word-list-resource-0115617/
5-letter words:
http://homepage.ntlworld.com/adam.bozon/Scrabble5.htm
6-letter words:
http://homepage.ntlworld.com/adam.bozon/Scrabble6.htm
7-letter words:
http://homepage.ntlworld.com/adam.bozon/Scrabble7.htm
8-letter words:
http://homepage.ntlworld.com/adam.bozon/Scrabble8.htm
Sample:
PIKES PIKIS PILAF PILAR PILAU PILAW PILEA PILED PILEI PILES PILIS
PILLS PILOT PILUS PIMAS PIMPS PINAS PINCH PINED PINES PINEY PINGO
PINGS PINKO PINKS PINKY PINNA PINNY PINON PINOT PINTA PINTO PINTS
PINUP PIONS PIOUS PIPAL PIPED PIPER PIPES PIPET PIPIT PIQUE PIRNS
PIROG PISCO PISOS PISTE PITAS PITCH PITHS PITHY PITON PIVOT PIXEL
PIXES PIXIE PIZZA PLACE PLACK PLAGE PLAID PLAIN PLAIT PLANE PLANK
PLANS PLANT PLASH PLASM PLATE PLATS PLATY PLAYA PLAYS PLAZA PLEAD
PLEAS PLEAT PLEBE PLEBS PLENA PLEWS PLICA PLIED PLIER PLIES PLINK
PLODS PLONK PLOPS PLOTS PLOTZ PLOWS PLOYS PLUCK PLUGS PLUMB PLUME
PLUMP PLUMS PLUMY PLUNK PLUSH PLYER POACH POCKS POCKY PODGY PODIA
POEMS POESY POETS POGEY POILU POIND POINT POISE POKED POKER POKES
With that you could generate a coupon code POACH72
Concatenating 2 words will increase the security posture of your system.
e.g. squirrel.nantucket.123
The Diceware page has a couple of long word lists, American and International. It also has a useful description of how to meet various levels of security.

Convert DOMElement to string and then strip unwanted tags

Now, thanks to community, one problem was fixed ( Delete all elements of a certain type from an XML doc using PHP ). Now I luckily hit other :)
So, I have XML file:
<piletilve_info>
<shows>
<show>
<description>
<lat>
<![CDATA[
LA CENERENTOLA <BR><BR>
("Cinderella")<BR><BR>
Opera buffo by Gioachino Rossini<BR><BR>
Music Director and Conductor: Arvo Volmer<BR>
Stage Director and Set Designer: Michiel Dijkema (Amsterdam)<BR>
Costumes: Claudia Damm (Berlin)<BR><BR>
Premiere on November 10, 2006<BR>
Approx running time: 3 h 20 min<BR>
Sung in Italian with subtitles in Estonian and English<BR><BR>
Who hasn’t heard the fairy-tale of Cinderella who crouched in the ashes, danced to the tune of her step-sisters but finally found the man of her dreams and a happy life. Why should this fairy-tale be told today?<BR>
An exciting and unexpected solution of the story of Cinderella with striking scenery and fantastic-modern costumes is presented by the Dutch stage director Michiel Dijkema and the German designer Claudia Damm.<BR>
In this story, an unhappy soul dreams of a prince on a white horse and of a better life, and women following the last trends chase the prince, and a man who considers himself very wise, is only interested in himself.<BR>
"Cinderella" is not just a fairy-tale, it includes reality as well as dreams, a lot of fantasies, comedy and grotesque. And all of this is expressed by the brilliant and virtuous music of Rossini!<BR>
]]>
</lat>
</description>
</show>
</shows>
<other node>
...
</other node>
</piletilve_info>
I navigate to my needed node using this code:
$books = $xpath->query('shows/show/description');
foreach($books as $description)
{
$description->removeChild($eng_lang);
$lat_lang = $description->getElementsByTagName('lat')->item(0);
}
I want to output modified $lat_lang elements , which is <[[CDATA ]]> parent, content using this mixed function:
$lat_lang->nodeValue = mb_convert_encoding(print_r(strip_tags_only($lat_lang->item(0)->nodeValue, TRUE),'<html><head><body>'), 'UTF-8' ,$encoding);
, but I fail and when I execute script on apache server, I get this error:
The XML page cannot be displayed
Cannot view XML input using XSL style
sheet. Please correct the error and
then click the Refresh button, or try
again later.
Only one top level element is allowed
in an XML document. Error processing
resource
'http://www.bilesuserviss.lv/lv_filtrs_...
Catchable fatal error: Object
of class DOMElement could not be
converted to string in /var/www/...
Could you help me finding solution?
Maybe the fact I do develop on delphi makes me this whole PHP<->XML coding more difficult that for normal people would .. :(
It was spelling problem.
Function name was strip_only_tags(), but I called it with strip_tags_only();
I am working on delphi and php<>xml project whole day .. Kinda feel tired already :(
Sorry all.
Also thansk to Gordon for commept. Kinda woke me up :D

Using regex to extract variables from a plain-text form letter?

I'm looking for a good example of using Regular Expressions in PHP to "reverse engineer" a form letter (with a known format, of course) that has been pasted into a multiline textbox and sent to a script for processing.
So, for example, let's assume this is the original plain-text input (taken from a USDA press release):
WASHINGTON, April 5, 2010 - North
American Bison Co-Op, a New Rockford,
N.D., establishment is recalling
approximately 25,000 pounds of whole
beef heads containing tongues that may
not have had the tonsils completely
removed, which is not compliant with
regulations that require the removal
of tonsils from cattle of all ages,
the U.S. Department of Agriculture's
Food Safety and Inspection Service
(FSIS) announced today.
For clarity, the fields that are variables are highlighted below:
[pr_city=]WASHINGTON, [pr_date=]April 5, 2010 - [corp_name=]North
American Bison Co-Op, a [corp_city=]New Rockford,
[corp_state=]N.D., establishment is recalling
approximately [amount=]25,000 pounds of [product=]whole
beef heads containing tongues that may
not have had the tonsils completely
removed, which is not compliant with
regulations that require [reason=]the removal
of tonsils from cattle of all ages,
the U.S. Department of Agriculture's
Food Safety and Inspection Service
(FSIS) announced today.
How could I efficiently extract the contents of the
pr_city
pr_date
corp_name
corp_city
corp_state
amount
product
reason
fields from my example?
Any help would be appreciated, thanks.
Well, a regex that works on your example could look like this (line breaks introduced to keep this beast legible, need to be removed prior to use):
/^(?P<pr_city>[^,]+), (?P<pr_date>[^-]+) - (?P<corp_name>.*?), a
(?P<corp_city>[^,]+), (?P<corp_state>[^,]+), establishment is
recalling approximately (?P<amount>.*?) of (?P<product>.*?),
which is not compliant with regulations that require (?P<reason>.*?),
the U\.S\. Department of Agriculture\'s Food Safety and Inspection
Service \(FSIS\) announced today\.$/
So, in PHP you could do
if (preg_match('/^(?P<pr_city>[^,]+), (?P<pr_date>[^-]+) - (?P<corp_name>.*?), a (?P<corp_city>[^,]+), (?P<corp_state>[^,]+), establishment is recalling approximately (?P<amount>.*?) of (?P<product>.*?), which is not compliant with regulations that require (?P<reason>.*?), the U\.S\. Department of Agriculture\'s Food Safety and Inspection Service \(FSIS\) announced today\.$/', $subject, $regs)) {
$prcity = $regs['pr_city'];
$prdate = $regs['pr_date'];
... etc.
} else {
$result = "";
}
This assumes a couple of things, for instance that there are no line breaks, and that the input is the entire string (and not a larger string from which this part has to be extracted from). I've tried to make assumptions about legal values that make some sense, but there is the very real chance that other inputs could break this. So some more test cases are probably needed.
If the surrounding text is constant, then something like this partial regex could do the trick:
preg_match('/^(.*?), (.*?)- (.*?), a (.*?), (.*?), establishment is recalling approximately (.*?), which is not compliant with regulations that require (.*?), the U.S. Department of Agriculture's Food Safety and Inspection Service (FSIS) announced today./', $text, $matches);
$matches[1] = 'WASHINGTON';
$matches[2] = 'April 5, 2010';
$matches[3] = ... etc...
If the surrounding text changes, then you're going to end up with a ton of false matches, no matches, etc... Essentially you'd need an AI to parse/understand PR releases.
Edit: Please disregard this crazy answer, as the other two are better. I should probably delete it, but I'm keeping it up for reference.
I have a crazy idea that just might work: build an XML string from the input by adding markups, then parse it. It might look something like this (completely untested) code:
preg_replace('([^,]*), ([^-]*)- ...etc...', '<pr_city>\1</pr_city><pr_date>\2</pr_date> ...etc...');
Parsing the XML afterwards is a needlessly complicated process that is best left to the PHP documentation: http://www.php.net/manual/en/function.xml-parse.php .
You could also consider converting it to JSON with this method, then using json_decode() to parse it. In any case, you have to think about what happens when " marks and > symbols appear in the input.
It might be easier to just match and remove one piece of the text at a time.

Categories