Getting some specific stuff out of a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Getting some specific stuff out of a file

Post by toasty2 »

With the following:

Code: Select all

<data type="page" name="home" pagetitle="Home">
I need to save name="home" to a string and pagetitle="Home" to a string, but I just need the contents of those, not the quotes and stuff. I'm creating my own format for storing information for a php script I'm making, but the code I displayed above CAN vary, causing the need to look for name=" and to start saving info to the string after the first quote, and to stop at the second quote, the same for pagetitle="". So, how do I do this? Since I am bad at explaining, this is the output I want:

Code: Select all

<?php
$page="home"
$pagetitle="Home"
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

quick and dirty

Code: Select all

preg_match_all('#\b([a-z0-9]+)\s*=\s*(["\'])(.*?)\\2#is', $yourText, $matches);
print_r($matches);
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I only want the first match, yours provides multiple matches if applicable, right?
I also can't just look for <data type="page" name="home" pagetitle="Home", because the way my script is going to work is that when someone goes to index.php?p=home, it's going to read the data file and look for <data type="page" name="$p"...
and it's going to need to save pagetitle's contents to a string, it will also look for the data after the data element and save it into a string and stop saving when it reaches </data>

I forgot to ask the whole question in the first post, it was too much for me to think about then, since I am bad at explaining usually.
Last edited by toasty2 on Sat Aug 12, 2006 11:14 am, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Are you writing XML?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code provided looks for the attributes you have, nothing more. It will return all that are found. (Provided it works.)
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Ambush Commander wrote:Are you writing XML?
Not really, but it is something similar looking, I got the idea from xml. :D
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, you should use XML. That will save you a lot of work.

For instance, what you just had, with a few tweaks:

Code: Select all

<dataset>
<data type="page" name="home" pagetitle="Home" />
</dataset>
Voila, valid XML.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I might try that, the data element I made has the actual page's contents saved between <data> and </data>, it's for a CMS. I was going to make my own format, because I'm not that knowledgeable on xml, each page in my CMS is going to have a <data type="page" name="name_of_page" pagetitle="the title of the page">The contents of the page go here</data>, so I have a question...do I make each page be inside it's own <dataset> </dataset> elements, or do I do like this?

Code: Select all

<dataset>
<data type="page" name="home" pagetitle="Welcome to our homepage">Page contents go here</data>
<data type="page" name="stuff" pagetitle="Stuff">Here is random stuff</data>
</dataset>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I'd recommend giving each page its own seperate XML file.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Aside from simplicity, why? What's wrong with one file? I was thinking it would be easier to manage, and to back it up if it was in one file, but I do wonder, what happens if the file gets really huge? Will php take really long to do stuff to it or read it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Say, for instance, you have ten pages. Assume each page has two to three kilobytes of text. That's 30K of page data for PHP to load plus the overhead of parsing the XML to find only 3K from it. Now consider having 100 pages.

Shall we say a single file has diminishing returns? Yes. Is a single file easier to manage? Maybe. Is a single file modular? It has some modularity, but not all that much. Is multiple files modular? More often, yes. If you could have several people updating pages at once, it's much better to have separate files. With possible growth of a site in mind, it's likely that there would be multiple editors.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I was thinking that too :)
Thanks

And there won't be multiple people editing pages with this cms, but the multiple files idea is still better. Also, in my hunt to think of ways to avoid using databases, I've thought of a secure way to store user data (inside a php file):

Code: Select all

<?php die("You do not have permission to view this file."); ?>
<!-- Data goes here -->
Php can only read that file (well, ftp clients and asp/other programs on the server could could, but a webbrowser can't) :D
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

feyd wrote:quick and dirty

Code: Select all

preg_match_all('#\b([a-z0-9]+)\s*=\s*(["\'])(.*?)\\2#is', $yourText, $matches);
print_r($matches);
Um, I'm finally trying out your code, but there's one problem, you can input the text to look for and get the output, but what string is it supposed to search in? If I can't specify the source, how am I going to search it? 8O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Wherever your tags are.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

feyd wrote:Wherever your tags are.
:?: Please elaborate, I didn't get that...How am I going to get the code to read a variable or a file?
Post Reply