Page 1 of 2
Getting some specific stuff out of a file
Posted: Sat Aug 12, 2006 10:58 am
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"
?>
Posted: Sat Aug 12, 2006 11:03 am
by feyd
quick and dirty
Code: Select all
preg_match_all('#\b([a-z0-9]+)\s*=\s*(["\'])(.*?)\\2#is', $yourText, $matches);
print_r($matches);
Posted: Sat Aug 12, 2006 11:08 am
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.
Posted: Sat Aug 12, 2006 11:14 am
by Ambush Commander
Are you writing XML?
Posted: Sat Aug 12, 2006 11:14 am
by feyd
The code provided looks for the attributes you have, nothing more. It will return all that are found. (Provided it works.)
Posted: Sat Aug 12, 2006 11:15 am
by toasty2
Ambush Commander wrote:Are you writing XML?
Not really, but it is something similar looking, I got the idea from xml.

Posted: Sat Aug 12, 2006 11:17 am
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.
Posted: Sat Aug 12, 2006 11:20 am
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>
Posted: Sat Aug 12, 2006 11:30 am
by Ambush Commander
I'd recommend giving each page its own seperate XML file.
Posted: Sat Aug 12, 2006 11:52 am
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?
Posted: Sat Aug 12, 2006 12:03 pm
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.
Posted: Sat Aug 12, 2006 12:07 pm
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)

Posted: Sun Aug 13, 2006 6:39 pm
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?

Posted: Sun Aug 13, 2006 6:45 pm
by feyd
Wherever your tags are.
Posted: Sun Aug 13, 2006 7:23 pm
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?