0

Very simply I have an file and on each line there is something like this

OneTwoThreeFourFiveSixSeven OneTwoThreeFourFiveSixSeven OneThreeFourSevenFiveSix
OneThreeFourSevenFiveSix OneTwoThreeFourFiveSixSeven OneThreeFourSevenFiveSix

how do I check if both of those lines have word "Five" in it?

I tried:

strstr();
strrpos();
strpos();

And others, but nothing gives me functionality I'm looking for. Is there some build in method in php or do I have to try and build function which would be looking for this?

Ok so this is the code:

foreach ($results as $result) {
    foreach ($lines as $line) {
        if(strpos($line, '$this->' . $result) || strpos($line, '($this->' . $result)){
            $i++;
        }
    }

    if($i == 0 && $result !== "__construct"){
        print_r($result);
    }
}
Jakub Zak
  • 1,212
  • 6
  • 32
  • 53
  • 1
    Can you show us the full code you tried? – Amal Murali Oct 23 '13 at 14:42
  • Those functions should give you what you need. What further information are you looking for? – aynber Oct 23 '13 at 14:42
  • 1
    Why don't those functions work? – andrewsi Oct 23 '13 at 14:42
  • Well if no one will be able to help me with this simple explanation of the functionality I'm looking for, than I will post it but its 80 lines of code and I need this functionality on last 2 lines, so for now I won't. But thanks – Jakub Zak Oct 23 '13 at 14:44
  • You only need to post the exact lines where you tried those functions. – aynber Oct 23 '13 at 14:45
  • @JakubZak - you're not passing in the right parameters, I think. `'$this->' . $result` is going to end up as a string starting off `$this->`, whereas I think you're trying to get the value of `$this->result`; you might want to try using `$this->$result`, I think. – andrewsi Oct 23 '13 at 14:49
  • I think there's something with file_get_contents() or so ... where $result = 'result', and the str..() functions are runned on a plain text program (i.e. on the source code printed somewhere), searching for the string '$this->' concatenated with the value of $result, ending to '$this->result' which is actually a string, if it's searching into a source code – Royal Bg Oct 23 '13 at 14:52
  • @andrewsi ok this script is going through file and separates all the functions names and adds them to array. Than this array is compared to several different files to find if certain functions were forgotten and are not being used, so the code can be cleaned. So I am sure I am passing correct parameters where $result is name of the function. – Jakub Zak Oct 23 '13 at 15:04
  • Thanks guys, I found my problem. :) Thx for all the suggestions. – Jakub Zak Oct 23 '13 at 15:12

1 Answers1

0

Try this code :

strrpos — Find the position of the last occurrence of a substring in a string

Returns FALSE if the needle was not found.

<?php
    $str1 = "OneTwoThreeFourFiveSixSeven";
    $str2 = "OneThreeFourSevenFiveSix";
    if(strrpos($str1, "Two") !== FALSE){
        echo 'Two is in $str1';
    }else{
        echo 'Two is not in $str1';
    }

    if(strrpos($str2, "Two") !== FALSE){
        echo 'Two is in $str2';
    }else{
        echo 'Two is not in $str2';
    }
?>

Tested in codepad : http://codepad.org/8KbwMq36

aynber
  • 22,380
  • 8
  • 50
  • 63
Donovan Charpin
  • 3,567
  • 22
  • 30
  • 1
    Might want to change it to `if(strrpos($str1, "Two") !== FALSE )` to prevent any 0 indexes throwing it off. – aynber Oct 23 '13 at 14:52
  • But lets say in each line there would be more than one string of the numbers like I showed before, will edit my original post so I can show you what I mean. – Jakub Zak Oct 23 '13 at 14:53
  • Thank's @aynber. So I think I can't help you anymore. I have written my answer before your edit.. – Donovan Charpin Oct 23 '13 at 15:02