Jump to content


Photo

Searching string in file, parse it and display results


  • Please log in to reply
3 replies to this topic

#1 Gogy

Gogy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 May 2006 - 06:55 PM

I am not PHP freak, but I have a problem. I need to solve a simple thing but I don't know how.

I am trying to do it for a two days now, but no success.

I was searching all over the internet and on this forum to but still nothing...

I found that it is the best to use explode function, but I don't get it how to do that all together.

I need simple .php file with one textBox (input name="number") and a submit button.

When you click on button, the PHP code should take the value from textBox and search the lines in file codes.txt and if the string in file exists, it should use explode function and show the number and serial found. No special restrictions in textBox.

File contains the numbers separated by space " ".
File content is something like this:

32434kj3l24234 09823kljk23j42k34
3322342344324 489237984732432
5646456456546 423432432423423
9090899823748 328948973892474
kj332423409894 kj324l34k2j4k234k

The pictures shows what I want.

Search box and button:
[img src=\"http://www2.shrani.si/files/1search596879.jpg\" border=\"0\" alt=\"IPB Image\" /]

If string is found:
[img src=\"http://www2.shrani.si/files/3found596881.jpg\" border=\"0\" alt=\"IPB Image\" /]

If string NOT found:
[img src=\"http://www2.shrani.si/files/2notfoun596880.jpg\" border=\"0\" alt=\"IPB Image\" /]

If somebody could make this for me, I would be grateful!

Thanks for reading!

P.S. Please don't forget that I need whole code for file and not only function. I tried to use the function but did not succeed [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 May 2006 - 07:02 PM

If you want someone to code this for you then you might want to take a trip to the PHP Freelancing forum as thats the place where you post requests for someone to do something for you.

But still what you want to do is relitively simple.

#3 Gogy

Gogy
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 May 2006 - 07:48 PM


Should I move it there or I can leave it here?

#4 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 31 May 2006 - 11:29 PM

I didn't test it so.... Maybe this will help you get started

<?PHP
$needle = $_POST['needle'];
$haystacks = file('codes.txt'); 
$num_recs = count($haystacks);
function find_needle($needle,$haystack) {
    if (!strstri($haystack, $needle)) {
        return false;
    }
    return true;
}
$i=0;
for($i=0;$i<$num_recs;$i++) {
    if(!find_needle($haystacks[$i],$needle)) {
        // do nothing
    }else{
        $code1 = $needle;
        $code2 = array_pop(explode(" ", $haystacks[$i]));
        $i = $num_recs+1;
    }
}
// display $code1 and $code2 however you desire

?>


this presumes the first set of numbers in each record is the needle/code1/the thing you are using to search


Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users