SitePoint Sponsor

User Tag List

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 87
  1. #51
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Could you be a bit more elaborate about that last post please? An error message without any context (what string did you use, what word were you looking for, have you changed anything since my last post, etc, etc) is rather vague.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  2. #52
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by gilgalbiblewheel View Post
    First, after the word is broken down into characters, it should search for the first letter throughout all the text.

    And then it should search for the 2nd letter and mark the distance and see if the 3rd letter would occur in the same distance, if not, go on to the next possible distance. If, success then, maybe we need an array marking the color in which the result should appear the letters and the position of the letters.
    If failure then the search should go on to the next first letter and try the same thing.
    Exactly!
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #53
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Could you be a bit more elaborate about that last post please? An error message without any context (what string did you use, what word were you looking for, have you changed anything since my last post, etc, etc) is rather vague.
    Never mind because the textboxes which I added later on solved that issue.
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #54
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Exactly! :Partier:
    What's wrong with this:
    Code php:
    $string="abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelove";
    $string_split=str_split($string);
    $chars=str_split("love");
     
    $first_letter=Array();
    $position=Array();
     
    for($i=0; $i<count($string_split); $i++){
    	if($chars[0]==$string_split[$i]){
    		$first_letter[]=$string_split[$i];
    		$position[]=strpos($string, $string_split[$i]);
    	}
     
    }
    print_r($first_letter);
    print_r($position);
    I keep getting 1s and 3s.
    edit:
    I stand corrected 1 is actually an L and 3 is the first position. But then how do you find out all the positions?
    Compare bible texts (and other tools):
    TheWheelofGod

  5. #55
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    What is it supposed to do?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #56
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    What is it supposed to do?
    Find the positions of the 1st letter of the word (which is the letter l) in the $string.
    Compare bible texts (and other tools):
    TheWheelofGod

  7. #57
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Your code makes no sense at all. Below is an outline of how to do what you want. Please read it carefully to see if you understand it. Don't just copy/paste and hope for the best.

    PHP Code:
    $charToSearchFor='l';
    $string='lovelovelovelovelove';
    $pos=0;
    $positions=array();
    $continue=true;
    while (
    $continue)
    {
       if (
    false !== ($newPos=strpos($string$charToSearchFor$pos)))
       {
          
    $pos=$newPos+1;
          
    array_push($positions$newPos);
       }
       else
       {
          
    $continue=false;
       }
    }
    var_dump($positions);
    /**
    array(5) {
      [0]=>
      int(0)
      [1]=>
      int(4)
      [2]=>
      int(8)
      [3]=>
      int(12)
      [4]=>
      int(16)
    }
    **/ 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #58
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    There are many ways to 'skin a cat', so I'll add one to ScallioXTX's solution.

    PHP Code:
    <?php
    $string 
    'lovelovelovelovelove';

    $positions = array();

    foreach(
    str_split($string) as $pos => $char){
      if(
    'l' === $char){
        
    array_push($positions$pos);
      }
    }

    var_dump(
      
    $positions
    );

    /*
      array(5) {
        [0]=>
        int(0)
        [1]=>
        int(4)
        [2]=>
        int(8)
        [3]=>
        int(12)
        [4]=>
        int(16)
      }
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #59
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    One for fun.
    PHP Code:
    <?php
    class CharacterFilter extends FilterIterator{
      protected
        
    $char;
      
      public function 
    __construct(Iterator $iterator$char){
        
    parent::__construct($iterator);
        
    $this->char $char;
      }
      
      public function 
    accept(){
        return 
    $this->char === parent::current();
      }
    }

    $it = new ArrayIterator(
      
    str_split('lovelovelovelovelove')
    );

    foreach(new 
    CharacterFilter($it'l') as $pos => $char){
      
    printf("Char '%s' found at position %d\n"$char$pos);
    }

    /*
      Char 'l' found at position 0
      Char 'l' found at position 4
      Char 'l' found at position 8
      Char 'l' found at position 12
      Char 'l' found at position 16
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #60
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Quote Originally Posted by AnthonySterling View Post
    One for fun.
    PHP Code:
    <?php
    class CharacterFilter extends FilterIterator{
      protected
        
    $char;
      
      public function 
    __construct(Iterator $iterator$char){
        
    parent::__construct($iterator);
        
    $this->char $char;
      }
      
      public function 
    accept(){
        return 
    $this->char === parent::current();
      }
    }

    $it = new ArrayIterator(
      
    str_split('lovelovelovelovelove')
    );

    foreach(new 
    CharacterFilter($it'l') as $pos => $char){
      
    printf("Char '%s' found at position %d\n"$char$pos);
    }

    /*
      Char 'l' found at position 0
      Char 'l' found at position 4
      Char 'l' found at position 8
      Char 'l' found at position 12
      Char 'l' found at position 16
    */
    First you want to hunt me down and now you want to skin a cat?
    Compare bible texts (and other tools):
    TheWheelofGod

  11. #61
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Your code makes no sense at all. Below is an outline of how to do what you want. Please read it carefully to see if you understand it. Don't just copy/paste and hope for the best.

    PHP Code:
    $charToSearchFor='l';
    $string='lovelovelovelovelove';
    $pos=0;
    $positions=array();
    $continue=true;
    while (
    $continue)
    {
       if (
    false !== ($newPos=strpos($string$charToSearchFor$pos)))
       {
          
    $pos=$newPos+1;
          
    array_push($positions$newPos);
       }
       else
       {
          
    $continue=false;
       }
    }
    var_dump($positions);
    /**
    array(5) {
      [0]=>
      int(0)
      [1]=>
      int(4)
      [2]=>
      int(8)
      [3]=>
      int(12)
      [4]=>
      int(16)
    }
    **/ 
    Code PHP:
    $string="abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelove";
    $string_split=str_split($string);
    $chars=str_split("love");
    $charToSearchFor='l';
     
    $pos=0;
    $positions=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[0], $pos))){
    		$pos=$newPos+1;
    		array_push($positions, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    var_dump($positions);
     
    $pos=0;
    $positions2=array();
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[1], $pos))){
    		$pos=$newPos+1;
    		array_push($positions2, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    var_dump($positions2);
     
    /*
    for($i=0; $i<count($positions); $i++){
    	echo $string_split[$positions[$i]]."<br />";
    	for($j=0; $j<count($positions); $j++){
    		echo $string_split[$positions[$i]]."<br />";
     
    	}
    }*/
    Why does the second while loop show an empty array?
    edit:
    Ok never mind. $continue had to be rewritten for the 2nd while loop.
    It's not finished yet but does this make any sense?
    Code PHP:
    $string="abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelove";
    $string_split=str_split($string);
    $chars=str_split("love");
    $charToSearchFor='l';
     
    $pos=0;
    $positions=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[0], $pos))){
    		$pos=$newPos+1;
    		array_push($positions, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    //var_dump($positions);
     
    $pos=0;
    $positions2=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[1], $pos))){
    		$pos=$newPos+1;
    		array_push($positions2, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    //var_dump($positions2);
     
    echo $string."<br />";
    for($i=0; $i<count($positions); $i++){
    	//echo $string_split[$positions[$i]]."<br />";
    	for($j=0; $j<count($positions2); $j++){
    		$distance=$positions2[$j]-$positions[$i];
    		//echo $distance.", ";
    		for($k=0; $k<count($chars); $k++){
    			if($string_split[$distance*$k]==$chars[2]){
    				echo "<strong>".$distance.", ".$string_split[$distance*$k]."</strong>";
    			}
    		}
    	}
    	echo "<br />";
    }
    Compare bible texts (and other tools):
    TheWheelofGod

  12. #62
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok never mind. $continue had to be rewritten for the 2nd while loop.
    It's not finished yet but does this make any sense?
    Code PHP:
    $string="abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelove";
    $string_split=str_split($string);
    $chars=str_split("love");
    $charToSearchFor='l';
     
    $pos=0;
    $positions=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[0], $pos))){
    		$pos=$newPos+1;
    		array_push($positions, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    //var_dump($positions);
     
    $pos=0;
    $positions2=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[1], $pos))){
    		$pos=$newPos+1;
    		array_push($positions2, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    //var_dump($positions2);
     
    echo $string."<br />";
    for($i=0; $i<count($positions); $i++){
    	//echo $string_split[$positions[$i]]."<br />";
    	for($j=0; $j<count($positions2); $j++){
    		$distance=$positions2[$j]-$positions[$i];
    		//echo $distance.", ";
    		for($k=0; $k<count($chars); $k++){
    			if($string_split[$distance*$k]==$chars[2]){
    				echo "<strong>".$distance.", ".$string_split[$distance*$k]."</strong>";
    			}
    		}
    	}
    	echo "<br />";
    }
    I'm not sure how to continue after the if statement within the for loop of $k.
    I want to see if the string_split[$distance*$k] meaning for example the 4th (4x1) letter after the 1st L is o and then the 8th (4x2) letter after L is v and then the 12th letter (4x3) is e. If so create an array and store them in the array. Otherwise go to the next $distance.
    Compare bible texts (and other tools):
    TheWheelofGod

  13. #63
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this makes sense:
    Code PHP:
    $string="abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelove";
    $string_split=str_split($string);
    $chars=str_split("love");
    $charToSearchFor='l';
     
    $pos=0;
    $positions=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[0], $pos))){
    		$pos=$newPos+1;
    		array_push($positions, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    echo "<br /><span style=\"color: red;\">".$chars[0]." = </span><br />";
    var_dump($positions);
     
    $pos=0;
    $positions2=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[1], $pos))){
    		$pos=$newPos+1;
    		array_push($positions2, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    echo "<br /><span style=\"color: red;\">".$chars[1]." = </span><br />";
    var_dump($positions2);
    $pos=0;
    $positions3=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[2], $pos))){
    		$pos=$newPos+1;
    		array_push($positions3, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    echo "<br /><span style=\"color: red;\">".$chars[2]." = </span><br />";
    var_dump($positions3);
    $pos=0;
    $positions4=array();
    $continue=true;
    while ($continue){
    	if (false !== ($newPos=strpos($string, $chars[3], $pos))){
    		$pos=$newPos+1;
    		array_push($positions4, $newPos);
    	}else{
    		$continue=false;
    	}
    }
    echo "<br /><span style=\"color: red;\">".$chars[3]." = </span><br />";
    var_dump($positions4);
     
    echo $string."<br />";
    for($i=0; $i<count($positions); $i++){
    	for($j=0; $j<count($positions2); $j++){
    		$distance=$positions2[$j]-$positions[$i];
    		//echo "<span style=\"color: red;\"> ".$distance.", </span>";
    		for($k=1; $k<count($chars)+1; $k++){
    			if($string_split[$distance*$k-1]==$chars[$k-1]){
    				echo "<span style=\"font-weight: bold;\">distance = <span style=\"color: blue; font-weight: bold;\">".$distance."</span>, <span style=\"font-weight: bold;\">letter</span> = <span style=\"color: red; font-weight: bold;\">".$string_split[$distance*$k-1]."</span>; ";
    			}
    		}
    	}
    	echo "<br />";
    }
    Output:
    Code:
    l =
    array(15) { [0]=> int(3) [1]=> int(14) [2]=> int(21) [3]=> int(28) [4]=> int(39) [5]=> int(46) [6]=> int(53) [7]=> int(64) [8]=> int(71) [9]=> int(78) [10]=> int(89) [11]=> int(96) [12]=> int(103) [13]=> int(114) [14]=> int(121) }
    o =
    array(16) { [0]=> int(5) [1]=> int(7) [2]=> int(16) [3]=> int(22) [4]=> int(32) [5]=> int(41) [6]=> int(47) [7]=> int(57) [8]=> int(66) [9]=> int(72) [10]=> int(82) [11]=> int(91) [12]=> int(97) [13]=> int(107) [14]=> int(116) [15]=> int(122) }
    v =
    array(15) { [0]=> int(11) [1]=> int(18) [2]=> int(23) [3]=> int(36) [4]=> int(43) [5]=> int(48) [6]=> int(61) [7]=> int(68) [8]=> int(73) [9]=> int(86) [10]=> int(93) [11]=> int(98) [12]=> int(111) [13]=> int(118) [14]=> int(123) }
    e =
    array(15) { [0]=> int(15) [1]=> int(20) [2]=> int(24) [3]=> int(40) [4]=> int(45) [5]=> int(49) [6]=> int(65) [7]=> int(70) [8]=> int(74) [9]=> int(90) [10]=> int(95) [11]=> int(99) [12]=> int(115) [13]=> int(120) [14]=> int(124) } abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelove
    distance = 4, letter = l; distance = 4, letter = o; distance = 4, letter = v; distance = 4, letter = e; distance = 29, letter = l; distance = 29, letter = o; distance = 29, letter = v; distance = 29, letter = e; distance = 54, letter = l; distance = 54, letter = o; distance = 79, letter = l; distance = 104, letter = l;
    distance = 8, letter = v; distance = 33, letter = v;
    
    distance = 4, letter = l; distance = 4, letter = o; distance = 4, letter = v; distance = 4, letter = e; distance = 29, letter = l; distance = 29, letter = o; distance = 29, letter = v; distance = 29, letter = e; distance = 54, letter = l; distance = 54, letter = o; distance = 79, letter = l;
    distance = 8, letter = v; distance = 33, letter = v;
    
    distance = 4, letter = l; distance = 4, letter = o; distance = 4, letter = v; distance = 4, letter = e; distance = 29, letter = l; distance = 29, letter = o; distance = 29, letter = v; distance = 29, letter = e; distance = 54, letter = l; distance = 54, letter = o;
    distance = 8, letter = v; distance = 33, letter = v;
    
    distance = 4, letter = l; distance = 4, letter = o; distance = 4, letter = v; distance = 4, letter = e; distance = 29, letter = l; distance = 29, letter = o; distance = 29, letter = v; distance = 29, letter = e;
    distance = 8, letter = v; distance = 33, letter = v;
    
    distance = 4, letter = l; distance = 4, letter = o; distance = 4, letter = v; distance = 4, letter = e;
    distance = 8, letter = v;
    Compare bible texts (and other tools):
    TheWheelofGod

  14. #64
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You are certainly on the right track, and your code makes sense, too!

    However (there's always that guy who has to say "however"), The problem with the code is that is written for 4 characters, so it won't work it you want to search for more than 4, or less than 4 characters.
    You'd need to rewrite it so that becomes generic and can look for any number of characters you feed it.
    Take a good look at the code, find the repetition across the creation of $positions up until $positions4, and replace that repetition by more generic code.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  15. #65
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You are certainly on the right track, and your code makes sense, too!

    However (there's always that guy who has to say "however"), The problem with the code is that is written for 4 characters, so it won't work it you want to search for more than 4, or less than 4 characters.
    You'd need to rewrite it so that becomes generic and can look for any number of characters you feed it.
    Take a good look at the code, find the repetition across the creation of $positions up until $positions4, and replace that repetition by more generic code.
    Well, I know that the while loop has to be within a for loop where $i<count($chars). But how would you handle the $positions? I think it should be in a sub array somehow.
    Compare bible texts (and other tools):
    TheWheelofGod

  16. #66
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Looking good!

    How about something like.
    PHP Code:
    <?php
    $string  
    'abclxoxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvx';
    $string .= 'xleoyvyeloveabclxxxoxxxvxxleoyvyeloveabclxxxoxxxvxxleoyvyelovee';

    $occurrences = array();

    $word 'love';

    foreach(
    str_split($string) as $pos => $chr){
      if(
    $word[0] !== $chr){
        continue;
      }
      if(
    $word === substr($string$posstrlen($word))){
        
    $occurrences[$pos] = array();
        for(
    $i 0$i strlen($word); $i++){
          
    $occurrences[$pos][ $word[$i] ] = $pos $i;
        }
      }
    }

    var_dump(
      
    $occurrences
    );

    /*
      array(5) {
        [21]=>
        array(4) {
          ["l"]=>
          int(21)
          ["o"]=>
          int(22)
          ["v"]=>
          int(23)
          ["e"]=>
          int(24)
        }
        [46]=>
        array(4) {
          ["l"]=>
          int(46)
          ["o"]=>
          int(47)
          ["v"]=>
          int(48)
          ["e"]=>
          int(49)
        }
        [71]=>
        array(4) {
          ["l"]=>
          int(71)
          ["o"]=>
          int(72)
          ["v"]=>
          int(73)
          ["e"]=>
          int(74)
        }
        [96]=>
        array(4) {
          ["l"]=>
          int(96)
          ["o"]=>
          int(97)
          ["v"]=>
          int(98)
          ["e"]=>
          int(99)
        }
        [121]=>
        array(4) {
          ["l"]=>
          int(121)
          ["o"]=>
          int(122)
          ["v"]=>
          int(123)
          ["e"]=>
          int(124)
        }
      }
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  17. #67
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Anthony, that doesn't take into account that the letters have to be equidistant does it? Or am I missing something?

    Quote Originally Posted by gilgalbiblewheel View Post
    Well, I know that the while loop has to be within a for loop where $i<count($chars). But how would you handle the $positions? I think it should be in a sub array somehow.
    Yes indeed!

    PHP Code:
    $string='lovexlxoxvxexxlxxoxxvxxe';
    $word='love';
    $chars=str_split($word);
    $positions=array();
    foreach (
    $chars as $i=>$char)
    {
      
    $pos=0;
      
    $continue=true;
      
    $positions[$i]=array();
      while (
    $continue) {
        if (
    false !== ($newPos=strpos($string$char$pos))){
            
    $pos=$newPos+1;
            
    array_push($positions[$i], $newPos);
        } else {
            
    $continue=false;
        }
      }
    }

    var_dump($positions);

    /**
    array(4) { 
      [0]=>
      array(3) { // Char 0 -- L
        [0]=>
        int(0)
        [1]=>
        int(5)
        [2]=>
        int(14)
      }
      [1]=>
      array(3) { // Char 1 -- O
        [0]=>
        int(1)
        [1]=>
        int(7)
        [2]=>
        int(17)
      }
      [2]=>
      array(3) { // Char 2 - V
        [0]=>
        int(2)
        [1]=>
        int(9)
        [2]=>
        int(20)
      }
      [3]=>
      array(3) { // Char 3 - E
        [0]=>
        int(3)
        [1]=>
        int(11)
        [2]=>
        int(23)
      }
    }
    **/ 
    Question is, what to do next?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  18. #68
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Anthony, that doesn't take into account that the letters have to be equidistant does it?
    Er, no.

    So, we need to find the first character of the word, then the second, and if the subsequent characters appear at the same distance away from each other it's a match?

    We use the second character as the indicator to judge the distance for the rest?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  19. #69
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Er, no.

    So, we need to find the first character of the word, then the second, and if the subsequent characters appear at the same distance away from each other it's a match?

    We use the second character as the indicator to judge the distance for the rest?
    Yes. Either that or build an array of all occurrences like I did in post #67 and check in there.
    Both methods should work and yield the same results

    Edit:


    Scratch that, the array method is messed up. Brownie points for anyone who sees why
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  20. #70
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sweet!

    It's my first working implementation and it needs refactoring, but you're more than welcome to it.

    PHP Code:
    <?php
    $string   
    'lovelovelovelove';
    $word     'love';
    $matches  = array();

    for(
    $pos 0$pos strlen($string); $pos++)
    {
      if(
    $word[0] !== $string[$pos])
      {
        continue;
      }
      for(
    $distance 1; ($pos $distance) < strlen($string); $distance++)
      {
        if(
    $word[1] !== $string[$pos $distance])
        {
          continue;
        }

        
    $search '';

        for(
    $i 0$i strlen($word); $i++)
        {
          
    $search .= isset($string[$pos + ($i $distance)]) ? $string[$pos + ($i $distance)] : '' ;
        }

        if(
    $word === $search)
        {
          
    $match = array();
          for(
    $i 0$i strlen($word); $i++)
          {
            
    $match[$string[$pos + ($i $distance)]] = $pos + ($i $distance) ;
          }
          
    array_push($matches$match);
        }
      }
    }

    print_r(
      
    $matches
    );

    /*
    Array
    (
      [0] => Array
      (
        [l] => 0
        [o] => 1
        [v] => 2
        [e] => 3
      )
      [1] => Array
      (
        [l] => 0
        [o] => 5
        [v] => 10
        [e] => 15
      )
      [2] => Array
      (
        [l] => 4
        [o] => 5
        [v] => 6
        [e] => 7
      )
      [3] => Array
      (
        [l] => 8
        [o] => 9
        [v] => 10
        [e] => 11
      )
      [4] => Array
      (
        [l] => 12
        [o] => 13
        [v] => 14
        [e] => 15
      )
    )
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  21. #71
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Sweet!

    It's my first working implementation and it needs refactoring, but you're more than welcome to it.

    PHP Code:
    <?php
    $string   
    'lovelovelovelove';
    $word     'love';
    $matches  = array();

    for(
    $pos 0$pos strlen($string); $pos++)
    {
      if(
    $word[0] !== $string[$pos])
      {
        continue;
      }
      for(
    $distance 1; ($pos $distance) < strlen($string); $distance++)
      {
        if(
    $word[1] !== $string[$pos $distance])
        {
          continue;
        }

        
    $search '';

        for(
    $i 0$i strlen($word); $i++)
        {
          
    $search .= isset($string[$pos + ($i $distance)]) ? $string[$pos + ($i $distance)] : '' ;
        }

        if(
    $word === $search)
        {
          
    $match = array();
          for(
    $i 0$i strlen($word); $i++)
          {
            
    $match[$string[$pos + ($i $distance)]] = $pos + ($i $distance) ;
          }
          
    array_push($matches$match);
        }
      }
    }

    print_r(
      
    $matches
    );

    /*
    Array
    (
      [0] => Array
      (
        [l] => 0
        [o] => 1
        [v] => 2
        [e] => 3
      )
      [1] => Array
      (
        [l] => 0
        [o] => 5
        [v] => 10
        [e] => 15
      )
      [2] => Array
      (
        [l] => 4
        [o] => 5
        [v] => 6
        [e] => 7
      )
      [3] => Array
      (
        [l] => 8
        [o] => 9
        [v] => 10
        [e] => 11
      )
      [4] => Array
      (
        [l] => 12
        [o] => 13
        [v] => 14
        [e] => 15
      )
    )
    */
    This one's good. BUt I need the negative results too. Such as:
    [l] => 12
    [o] => 9
    [v] => 6
    [e] => 3
    I don't understand where you're splitting the $word or $string? But it works.
    Compare bible texts (and other tools):
    TheWheelofGod

  22. #72
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gilgalbiblewheel View Post
    This one's good.
    Thanks.

    Quote Originally Posted by gilgalbiblewheel View Post
    ... I need the negative results too.
    I don't recall you stating you needed this functionality.

    Either way, I'm going to leave that, minor issue, up to you to solve.

    Quote Originally Posted by gilgalbiblewheel View Post
    I don't understand where you're splitting the $word or $string? But it works.
    I don't split them.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  23. #73
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a function that turns a word backwards? Or is there a better solution than reusing this method only backward?
    Compare bible texts (and other tools):
    TheWheelofGod

  24. #74
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gilgalbiblewheel View Post
    Is there a function that turns a word backwards? Or is there a better solution than reusing this method only backward?
    Seriously? You created a new post rather than search the manual, after all your/our work?

    Barmy.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  25. #75
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Seriously? You created a new post rather than search the manual, after all your/our work?

    Barmy. :confused:
    ok. How do you search if php.net has such a function? What is it called?
    Compare bible texts (and other tools):
    TheWheelofGod


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •