Forum Moderators: coopster
I'm trying to retrieve the information from the text file using wildcard, but could not think of any idea on how to start. Can someone help me out.
I have a set of data on text file{shown below}, and i want to search it using wild card and then retrieve the data from there.
Text File is in this format:
NIDStart-23423-NIDEnd VIDStart-53453-VIDEnd NIDStart-24563-NIDEnd VIDStart-07983-VIDEnd NIDStart-12323-NIDEnd
Input: NIDStart-*-NIDEnd
Output:
23423
24563
12323
$pattern = "/NIDStart-(\d+)-NIDEnd/";
$matches = array();
[url=http://www.php.net/preg_match]preg_match[/url]($pattern, $string, $matches);
echo '<pre>'; print_r($matches); echo '</pre>';
try this. Oh, and Welcome to WebmasterWorld! :)
I slightly tried to modify it, but i can't get it the way i wanted
1. I tried to built the $pattern by using str_replace, that replaced all the * of the $query with "(\w+)" and added "/" to either end
This worked very well, but $query="NIDStart*NIDEnd*Test"; didn't work and I had to use $query="NIDStart-*-NIDEnd-*-Test"; though i was expecting the output with "-" sign
2. Can i run similar query with HTML tag ?
eg: $query="<table border=1>*</table> to get everything between that tag ? It didn't work for me either
<?php
$string="NIDStart-23a423-NIDEnd-222-Test NIDStart-24563-NIDEnd-555-Test NIDStart-12323-NIDEnd ";
$query="NIDStart-*-NIDEnd-*-Test";
$pattern=(str_replace("*","(\w+)",$query));
$pattern="/" . $pattern . "/";
$matches = array();
preg_match_all($pattern, $string, $matches);
foreach ($matches[0] as $item) {
echo ($item)."<hr/>";
}
?>
You should be using \d+, otherwise it looks like it should work. Also, just in case you change your query to something with characters that need to be escaped, it should read like this:
$pattern = '/' . str_replace("*", "(\w+)", [url=http://www.php.net/preg-quote]preg_quote[/url]($query, '/')) . '/';
>> Can i run similar query with HTML tag ?
Yes you can. Using the updated code above the preg_quote should properly escape the HTML and it should work. Try it out.