Jump to content

PHP, XML and Regular Expressions


verus

Recommended Posts

I have run into a problem and I can't seem to figure it out.  It is probably simple, but I am fairly new to Web Development.  Basically what I am trying to do is search an xml document, but I don't even know where or how I would put the regular expression.  I will explain more after the code (I know, this is probably really ugly and weird code):

 

<?php
    $search = trim($_POST['search']);
    $xml = simplexml_load_file('Documents.xml');
    $i=0;
?>
<html>
<head></head>
<body>
<h1>Search Results for: <?php print $search; ?> </h1>
<table>
<?php
   foreach ($xml->document as $document) {
        if($search == (string) $document->documentName) {
        ?>
        <tr><td><?php print $document->documentName; ?></td><td><a href="<?php print $document->documentLocation; ?>"><?php print $document->documentLocation; ?></a></td></tr>
        <?php $i++;
        } } ?>
        </table>        
<?php
        if($search !=(string) $document->documentName) {
            while($i == 0) { 
            $i++;
            print 'No Results for ' . $search . ' found.';
} }    
?>
</body>
</html>

 

I have it so it will search the XML file and pull out anything that matches $search, but what I am trying to figure out is how I would 1) write a regular expression for $search.  I am thinking something simple like just throwing % in the beginning and end of the variable.  More importantly 2) where I would put it so that it will search the regular expression with the variable rather than just the variable.

 

Any help would be greatly appreciated.  Thanks.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.