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
https://forums.phpfreaks.com/topic/153108-php-xml-and-regular-expressions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.