How to search by skipping equidistant letters

I want to search word within a string by equidistant letters pattern:
Example I want to search love within the x’s at a skip of 3 letters or every 4th letter:

xxxlxxxoxxxvxxxexxx

My logic is that first I need to explode the keyword:

str_split($keyword);

But I’m not sure afterward. But it’s not splitting the into hebrew letters, rather a black diamond shape with a ?.
So far I have this:

<?php
for($tdid=0; $tdid < count($id); $tdid++){
	$strText=stripslashes(mysql_escape_string($textData[$tdid]));
	include("../includefiles/vowelmarks.php");
	for($i=0; $i<count($vm); $i++){
		//echo $vm[$i];
		$strText=str_replace($vm[$i], "", $strText);
	}
	$letters=str_split($strText);
	for($i=0; $i<count($letters); $i++){
		if($letters[$i]!==" "){
			if($i==0 or ($i%2)==22){
?><tr><?php
			}
			//include("../includefiles/highlight_getBibleCode.php");
			echo "<td id=\\"regular[]\\" style=\\"font-size: 14px; direction: rtl; font-family:'SBL Hebrew';\\">".$letters[$i]."</td>\
";
			if(($i%2)==22){
?></tr><?php
			}
		}
	}
}

unless I’m misunderstanding something, why can’t you just explode the string using ‘xxx’ as the delimeter and then concatenate every 2nd element of the returned array, if it is a letter, to get the embedded word?

I want to conduct a search of how many of the keyword can you find in:

xxxlxxxoxxxvxxleoxvxex

if $keyword=love?

Now you have changed the criteria in which words can be embedded in the x’s. In your op the letters were spaces a constant 3 x’s apart and your thread title says the letters are equidistant.

Now you are showing the embedded words can overlap each other and that the letters don’t have to be equidistant apart. There’s too much uncertainty in your posted criteria so I think I’ll leave this one alone.

This looks like code to do a crossword puzzle. To verify that a word doesn’t appear twice in a string. But you have letters between your letters? Maybe you can instead explode the search term into individual letters and then search your xxxlxxxoxxxvxxleoxvxex string for each time an l appears if an o appears after then check if a v appears after, etc…

If not than I misunderstood this problem.

gilgalbiblewheel, can you give some real examples of the strings you want to work with, values to look for, demonstrations of what you want to find?

that’s right like a crossword puzzle. I pretty much figured it out, that the text given is split up into a huge array of letters. And the search ought to look for two letters at a certain number apart and try to match another latter with the same number otherwise it should skip or exit and continue with a new search.

Take a look at my website:
http://www.gbgrafix.com/thewheelofgod/bible-code-search/
When you make your selections either type in a word in hebrew by using the keyboard which opens upon clicking in the one of the textareas on the left.

Or select the book and chapter and select a word and drag and drop in the textarea and click the same chapter to refresh it.

I’ve tried to figure out if every letter will be made bold and red which are found in the keyword but it didn’t. It made some of the bold and red in the table.
the Keyword(s):

echo "Words Searched For: <ol>";
$keyword_letters=Array();
$into_heb_letters=Array();
for($key=0; $key<count($COLORS); $key++){
	if($textarea[$key]!=""){
		echo "<li><span style=\\"color:".$COLORS[$key]."; font-weight: bold;\\">".$textarea[$key]."</span>";
		$num = $key+1;
		if($textarea[$num]!=""){
			echo " + ";
		}
		echo "</li>";
		$strKeyword=$textarea[$key];
		for($i=0; $i<count($alephbet); $i++){
			$strKeyword=str_replace($alephbet[$i], $alphabet[$i], $strKeyword);
		}
		$strKeyword=str_replace(" ", "", $strKeyword);
		$into_letters=str_split($strKeyword);
		/* may be useful if willing to joing the keywors together
		for($i=0; $i<count($into_letters); $i++){
			$keyword_letters[]=$into_letters[$i];
		}
		*/
		for($eng=0; $eng<count($into_letters); $eng++){
			for($heb=0; $heb<count($into_alephbet); $heb++){
				if($alphabet[$heb]==$into_letters[$eng]){
					$into_heb_letters[]=$into_alephbet[$heb];
				}
			}
		}
	}
}
echo "</ol>";

I had to convert to english letters and reconvert them after the split();
So the letters in hebrew are in $into_heb_letters array without a problem.

No this is more complicated:

$chap_letters= Array();
$chap_heb_letters= Array();
for($tdid=0; $tdid < count($id); $tdid++){
	$strText=stripslashes(mysql_escape_string($textData[$tdid]));
	include("../includefiles/vowelmarks.php");
	for($i=0; $i<count($vm); $i++){
		$strText=str_replace($vm[$i], "", $strText);
	}
	for($i=0; $i<count($alephbet); $i++){
		$strText=str_replace($alephbet[$i], $alphabet[$i], $strText);
	}
	$strText=str_replace(" ", "", $strText);
	$letters=str_split($strText);
	//print_r($letters);
	for($i=0; $i<count($letters); $i++){
		//include("../includefiles/highlight_getBibleCode.php");
		$chap_letters[]=$letters[$i];
	}
}
for($i=0; $i<count($chap_letters); $i++){
	$num=$i+1;//for the sake of the tr problem
	if($i==0){
		echo "<tr>\
";
		//echo "<td style=\\"border: 1px solid black; font-size: 14px;\\">".$num."</td>\
";
	}
	$td="<td id=\\"td_".$num."\\" style=\\"border: 1px solid black; font-size: 14px;".$hebrew_font."\\">".$chap_letters[$i]."</td>\
";
	for($j=0; $j<count($into_alephbet); $j++){
		$td=str_replace(">".$alphabet[$j]."</", ">".$into_alephbet[$j]."</", $td);	
		$chap_heb_letters[]=$into_alephbet[$j];
	}
	for($k=0; $k<count($into_heb_letters); $k++){
		if($chap_heb_letters[$i]==$into_heb_letters[$k]){
			$td=str_replace("\\">".$chap_heb_letters[$i]."</", " color: red;\\">".$chap_heb_letters[$i]."</", $td);
		}
	}
	echo $td;

	if($i!==0 && $num % $skiprow == 0){
		echo "</tr>\
";
		if($i<(count($letters)-1)){
			echo "\
";
			echo "<tr>\
";
			//echo "<td style=\\"border: 1px solid black; font-size: 14px;\\">".$num."</td>\
";
		}
	}
}

I’ll explain more about this in my next post.

    for($j=0; $j<count($into_alephbet); $j++){
        $td=str_replace(">".$alphabet[$j]."</", ">".$into_alephbet[$j]."</", $td); 
        $chap_heb_letters[]=$into_alephbet[$j];
    }
    for($k=0; $k<count($into_heb_letters); $k++){
        if($chap_heb_letters[$i]==$into_heb_letters[$k]){
            $td=str_replace("\\">".$chap_heb_letters[$i]."</", " color: red;\\">".$chap_heb_letters[$i]."</", $td);
        }
    }
    echo $td; 

$into_alephbet reconverts the english letters of the keyword into hebrew with str_replace();
$chap_heb_letters are all the hebrew letters in the chapter. My intention of writing the last for loop is that I wanted (to begin with) to see if all the letters of the keyword would be colored red in the grid.

But I intend to search if there’s any match of equidistant hebrew letters of the keyword(s) in the given hebrew text.

What I’m looking for is that I want to search a $keyword like love within the string.

  1. My logic is that I have to break the string into an array of letters;
  2. break the $keyword into and array of letters
  3. create a for loop to search
  4. make the array of the count($keyword)*$i not exceed the count($string) else exit the loop
  5. here’s where I find tricky: I need to match all the letters of the keyword to the letters of the string at the given equidistant letter sequence or $els which is a variable, or maybe use the $i from the for loop instead.

I have a question about the if statement:
can i bring in an array within the if brackets:

if($kw[$k]==$str[$i] && $kw[$k]==$str[$i]...depending at the number of $key){
...}

I can’t figure it out so far:

$els=mysql_escape_string($_GET["els"]);
$word=mysql_escape_string($_GET["word"]);
$string = "xxxlxxxoxxxvxxxexxxylyoyvye";

$split_string = str_split($string);
$split_word = str_split($word);
$searchmax=floor(count($split_string)/count($split_word));

for($j=0; $j<count($split_word); $j++){
	for($i=0; $i<$searchmax; $i++){
		if($split_word[$j]==$split_string[$i]){
			$string=str_replace($split_string[$i], "<span style=\\"color: red;\\">".$split_string[$i]."</span>", $string);
			$result=Array();
			for($skip=0; $skip<$searchmax; $skip++){
				if($split_word[$j+$skip]==$split_string[$i+$skip]){
					$string=str_replace($split_string[$i+$skip], "<span style=\\"color: red;\\">".$split_string[$i+$skip]."</span>", $string);
				}
				$result[] = substr($string, $skip);
			}
			//print_r($result);
			
		}
	}
}
/*
$result = (string)NULL;
$done = false;

$string = substr($string,$els);

$result .= $split[0];
while(!$done) {

    $string = substr($string,4);
    $split = str_split($string);
    $result .= $split[0];
    
    if(empty($string))
        $done = true;
}
*/
echo $string;

strpos(); seems to be essential to know where to start.

		if($split_word[$j]==$split_string[$i]){
			$findme=$split_word[$j];
			$pos=strpos($string, $findme);

shows the first letter starts at position 3. But from that point it’s not searching the 2-4th letters in the string.

$els=mysql_escape_string($_GET["els"]);
$word=mysql_escape_string($_GET["word"]);
$string = "abclxxxoxxxvxxxexxxylyoyvye";

$split_string = str_split($string);
$split_word = str_split($word);
$searchmax=floor(count($split_string)/count($split_word));

for($j=0; $j<count($split_word); $j++){
	for($i=0; $i<$searchmax; $i++){
		if($split_word[$j]==$split_string[$i]){
			$findme=$split_word[$j];
			$pos=strpos($string, $findme);
			echo $pos;
			//$string=str_replace($split_string[$i], "<span style=\\"color: red;\\">".$split_string[$i]."</span>", $string);
			$result=Array();
			for($skip=0; $skip<$searchmax; $skip++){
				$int = $pos+$skip;
				if($split_word[$j+$skip]==$split_string[$int]){
					$result[$int] = $split_string[$int];
					//$string=str_replace($split_string[$int], "<span style=\\"color: red;\\">".$split_string[$int]."</span>", $string); 
				}
				//$result[] = substr($string, $skip);
			}
			print_r($result);
		}
	}
}
/*
$result = (string)NULL;
$done = false;

$string = substr($string,$els);

$result .= $split[0];
while(!$done) {

    $string = substr($string,4);
    $split = str_split($string);
    $result .= $split[0];
    
    if(empty($string))
        $done = true;
}
*/
echo $string;

Well half the problem seems to be solved:

<input id="number" type="text" value="all" />
<input id="word" type="text" value="love" />
<a href="JavaScript: ext='?els='+document.getElementById('number').value+'&word='+document.getElementById('word').value; window.location=ext;">search this</a><br /><br />
<?php
$els=mysql_escape_string($_GET["els"]);
$word=mysql_escape_string($_GET["word"]);
$string = "abclxxxoxxxvxxxexxxylyoyvye";

$split_string = str_split($string);
$split_word = str_split($word);
$searchmax=floor(count($split_string)/count($split_word));

$word_pos=Array();

for($j=0; $j<count($split_word); $j++){
	$word_pos[]=strpos($string, $split_word[$j]);
}
print_r($word_pos);
$j=0;
for($i=0; $i<count($split_string); $i++){
	if($i==$word_pos[$j]){
		$str.="<span style=\\"color: red;\\">".$split_string[$i]."</span>";
		$j++;
	}else{
		$str.=$split_string[$i];	
	}	
}
		
echo $str;
?>

This returns:

abclxxxoxxxvxxxexxxylyoyvye

What I don’t understand is why the other part is omitted. I would like that to be highlighted in another color like blue.

abclxxxoxxxvxxxexxxylyoyvye

Your function doesn’t work. Sure it seems to be working because it colors “love” in the string, but if you only have L, O and V in the string (no E) it will also color LOV, and that’s not supposed to happen. It should color the whole word or nothing at all.

This works:


<?php
$els=isset($_GET['els']) ? $_GET['els'] : '';
$word=isset($_GET['word']) ? $_GET['word'] : '';
$string="abclxxxoxxxvxxxexxxylyoyvye";

$colors=array('red', 'blue');
$string_split=str_split($string);
$chars=str_split($word);
$strlen=strlen($string);
$instances=array();
$continue=true;
$pos=0;

while ($continue)
{
	$instance=array();
	foreach($chars as $char)
	{
		if (($newPos=strpos($string, $char, $pos)) !== false)
		{
			array_push($instance, $newPos);
			$string=substr_replace($string, '#', $newPos, 1);
			$pos=$newPos;
		}
		else
		{
			$continue=false;
			break;
		}
	}
	if (count($instance)==count($chars))
	{
		array_push($instances, $instance);
		$pos=0;
	}
}

foreach ($instances as $i=>$instance)
{
	$color=$colors[$i];
	foreach($instance as $char)
	{
		$string_split[$char]='<strong style="color: '.$color.';">'.$string_split[$char].'</strong>';
	}
}

var_dump(implode('', $string_split));

For “abclxxxoxxxvxxxexxxylyoyvye” it outputs abclxxxoxxxvxxxexxxylyoyvye

It can also detect interwoven words.
For example for “xxlxlxxoxoxvxxvxexxe” it outputs xxlxlxxoxoxvxxvxexxe

Please take a look at the code and see if you can follow what it does. If there is anything you can’t quite follow don’t hesitate to ask! :slight_smile:

Warning: strpos() [function.strpos]: Empty delimiter in \examples\mybcode3.php on line 28

There’s an error

Did you provide $_GET[‘word’] ? If not you do indeed get that error.

That’s right I forgot about that. Thanks!

where can I put the <td> tags for the non-highlighted letters? The $els should determine the amount of tds per tr.

I don’t follow. How do you want <td>s in here? What have you tried yourself? Do you understand the code I posted?

http://www.gbgrafix.com/thewheelofgod/bible-code-search/
Except that I intend to replace your code instead of this.