Forum Moderators: coopster

Message Too Old, No Replies

Find Wildcard

Identifying the wildcard content

         

haamro

12:30 pm on Jan 8, 2009 (gmt 0)

10+ Year Member



Hi there,

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

eelixduppy

2:38 pm on Jan 8, 2009 (gmt 0)



Gonna have to use regular expressions for this one:

$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! :)

haamro

9:39 am on Jan 9, 2009 (gmt 0)

10+ Year Member



Hi eelixduppy, thank u for ur reply, it worked with charm.

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/>";
}

?>

eelixduppy

4:11 pm on Jan 9, 2009 (gmt 0)



>> \w+

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.