PHP - Sub-string words of a string - php

I need to find substrings of a given string but the substrings must be a word in the English language.
I.E. Given string = every, then substrings will be "ever", "very" and etc.
Thanks for the help.

You will need two things. First, you have to find all possible substrings. Then you will need a list with all English words (there are many free compilations).
This is a possible implementation:
$result = array();
$len = strlen($string)
for ($i = 0; $i < $len; $i++) {
for ($j = 1; $j <= $len - $i; $j++) {
$substring = substr( $string , $i , $j );
if ( is_an_english_word( $substring ) )
$result[] = $substring;
}
}

Related

How can I grab the index of where the longest identical consecutive portion of a string begins

I'd like the code to output index 6 since d is the starting point in terms of the longest identical consecutive portion in the string.
Not sure what I'm doing wrong here but it's currently returning 3 instead. Seems like I'm going in the right direction but something is missing but I can't pinpoint what.
Any feedback is appreciated! :)
$str = "abbcccddddcccbba";
$array = preg_split('/(.)(?!\1|$)\K/', $str);
$lengths = array_map('strlen', $array);
$maxLength = max($lengths);
$ans = array_search($maxLength, $lengths); // returns 3 but need it to return 6
echo $ans;
$lengths = array_map('strlen', $array);
Above line has only lengths of adjacent similar characters. array_search on max of those lengths will only yield the index where the maximum length is stored. It is totally unrelated with getting the index 6 of your string. If you still wish to get it, you will have to array_sum till that index to get the start index in the actual string.
Snippet:
<?php
$str = "abbcccddddcccbba";
$array = preg_split('/(.)(?!\1|$)\K/', $str);
$lengths = array_map('strlen', $array);
$maxLength = max($lengths);
array_splice($lengths,array_search($maxLength, $lengths));
$ans = array_sum($lengths);
echo $ans;
Online Demo
Alternate Solution:
I would write a simple for loop that uses 2 pointers to keep track of start index of similar characters and record the frequency and start index whenever it is greater than max frequency.
Snippet:
<?php
$str = "abbcccddddcccbba";
$len = strlen($str);
$maxF = 1;
$maxIdx = $startIdx = 0;
for($i = 1; $i < $len; ++$i){
if($str[ $i ] != $str[ $i - 1] || $i === $len - 1){
if($str[ $i ] === $str[ $i - 1] && $i === $len - 1) $i++;
if($maxF < $i - $startIdx){
$maxF = $i - $startIdx;
$maxIdx = $startIdx;
}
$startIdx = $i;
}
}
echo $maxIdx;
Online Demo

PHP Explode Show Seperator

So I wrote the following code to show the words after the fourth full stop / period in a sentence.
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$minText = explode(".", $text);
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i];
}
The algorithm is working and it is showing me the rest of the sentence after the fourth "." full stop / period.... My problem is that the output is not showing the full stops in the sentence therefore it is showing me just text without the proper punctuation "." .... Can someone please help me out on how to fix the code to display also the full stops / periods ??
Thanks a lot
you could try this...
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i].".";
}
notice the added period at the end of the echo command // .".";
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$minText = explode(".", $text);
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i].".";
}
Instead of splitting the input string and then iterating over it, you can find the nth position of the separator (.) in the string by using strpos() function by changing the offset parameter.
Then, it is just the matter of printing the sub-string from the position we just determined.
<?php
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$pos = 0;
//find the position of 4th occurrence of dot
for($i = 0; $i < $limit; $i++) {
$pos = strpos($text, '.', $pos) + 1;
}
print substr($text, $pos);
If desired output is "seperated.with.full.stops.", then you can use:
<?php
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
$minText = explode(".", $text);
$minText = array_slice($minText, $limit);
echo implode('.', $minText) . '.';
If you want to break it up on the periods between words, but keep the one at the end as actual punctuation, you may want to use preg_replace() to convert the periods to another character and then explode it.
$text = "this.is.the.message.seperated.with.full.stops.";
$limit = 4;
//replace periods if they are follwed by a alphanumeric character
$toSplit = preg_replace('/\.(?=\w)/', '#', $text);
$minText = explode("#", $toSplit);
for($i = $limit; $i < count($minText); $i++){
echo $minText[$i] . "<br/>";
}
Which Yields
seperated
with
full
stops.
Of course, if you just simply want to print all the full stops, then add them in after you echo the term.
echo $minText[$i] . ".";

How to split a string with dynamic integer delimiter inside this string in PHP?

Hex string looks like:
$hexString = "0307wordone0Banotherword0Dsomeotherword";
$wordsCount= hexdec(substr($hexString , 0, 2));
First byte (03) is total number of words in string. Next byte is count for characters of the first word (07). And after 7 bytes there is another integer 0B which tells that next word length is 11 (0B) characters, and so on...
What should function for exploding such string to array look like? We know how many iterations there should be from $wordsCount. I've tried different approaches but nothing seems to work.
This can be parsed with a simple for loop in O(n). No need for some fancy (and slow) regex solutions.
$hexString = "0307wordone0Banotherword0Dsomeotherword";
$wordsCount = hexdec(substr($hexString, 0, 2));
$arr = [];
for ($i = 0, $pos = 2; $i < $wordsCount; $i++) {
$length = hexdec(substr($hexString, $pos, 2));
$arr[] = substr($hexString, $pos + 2, $length);
$pos += 2 + $length;
}
var_dump($arr);
you can solve this by iterating a pointer on the string with a for loop.
$hexString = "0307wordone0Banotherword0Dsomeotherword";
$wordsCount= hexdec(substr($hexString , 0, 2));
$pointer = 2;
for($i = 0; $i<$wordsCount;$i++)
{
$charCount =hexdec(substr($hexString , $pointer, 2 ));
$word = substr($hexString , $pointer + 2, $charCount);
$pointer = $pointer + $charCount + 2;
$words[] = $word;
}
print_r($words);

How can I calculate the sum of "letter numbers" in a string?

Is there any built-in PHP function through which I can count the sum of indexes of letters of the alphabet found in a string?
<?php
$a = "testword";
echo "Count of Characters is: " . strlen($a);
?>
Now I want to get a cumulative "total" of this word.
e.g.
A is the first letter of the alphabet so it maps to 1
B is the second letter of the alphabet so it maps to 2
C is the third letter of the alphabet so it maps to 3
D is the fourth letter of the alphabet so it maps to 4
So the word ABCD gives 1+2+3+4=10
Similarly I need a function for "testword" or any word.
function WordSum($word)
{
$cnt = 0;
$word = strtoupper(trim($word));
$len = strlen($word);
for($i = 0; $i < $len; $i++)
{
$cnt += ord($word[$i]) - 64;
}
return $cnt;
}
var_dump(WordSum("testword"));
Just to show a totally different method, for the sheer pleasure of demonstrating some of PHP's array functions:
$data = "testword";
$testResult = array_values(array_merge(array_fill_keys(range('A','Z'),
0
),
array_count_values(str_split(strtoupper($data)
)
)
)
);
$wordCount = 0;
foreach($testResult as $letterValue => $letterCount) {
$wordCount += ++$letterValue * $letterCount;
}
var_dump($wordCount);
$a = "test";
$b = "word";
echo (strlen($a) + strlen($b));

PHP function to loop thru a string and replace characters for all possible combinations

I am trying to write a function that will replace characters in a string with their HTML entity encoded equivalent.
I want it to be able to go through all the possible combinations for the given string, for example:
go one-by-one
then combo i.e.. 2 at a time, then three at a time, till you get length at a time
then start in combo split, i.e.. first and last, then first and second to last
then first and last two, fist and second/third last
So for the characters "abcd" it would return:
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
etc.......... so on and so forth till there are no other combinations
Any ideas, or has anyone seen a function somewhere I could modify for this purpose?
loop from 0 to 2^length - 1. On each step, if Nth bit of the loop counter is 1, encode the Nth character
$str = 'abcd';
$len = strlen($str);
for($i = 0; $i < 1 << $len; $i++) {
$p = '';
for($j = 0; $j < $len; $j++)
$p .= ($i & 1 << $j) ? '&#' . ord($str[$j]) . ';' : $str[$j];
echo $p, "\n";
}
There are 2^n combinations, so this will get huge fast. This solution will only work as long as it fits into PHP's integer size. But really who cares? A string that big will print so many results you'll spend your entire life looking at them.
<?php
$input = 'abcd';
$len = strlen($input);
$stop = pow(2, $len);
for ($i = 0; $i < $stop; ++$i)
{
for ($m = 1, $j = 0; $j < $len; ++$j, $m <<= 1)
{
echo ($i & $m) ? '&#'.ord($input[$j]).';' : $input[$j];
}
echo "\n";
}
How about this?
<?php
function permutations($str, $n = 0, $prefix = "") {
if ($n == strlen($str)) {
echo "$prefix\n";
return;
}
permutations($str, $n + 1, $prefix . $str[$n]);
permutations($str, $n + 1, $prefix . '&#' . ord($str[$n]) . ';');
}
permutations("abcd");
?>

Categories