PHP count and sum preg_matches - php

So... I have a problem with counting all ccurrences of preg_match and summing/addition first parts of it (number before "/"). I just want to get the average of numbers before "/". Really sorry for bad english.
script.php
$wyniki=file("wyniki.txt");
foreach($wyniki as $w)
{
preg_match("/^([0-9]{1})\/([0-9]{1})$/",$w,$ar);
if(!empty($ar)){
print_r($ar[1].'/'.$ar[2]);
echo("\n");
}
}
script2.php (fail, but other way of script.php)
$file=fopen("wyniki.txt", "r");
$read=fread($file, filesize("wyniki.txt"));
echo($read."\n");
//if($read!=trim(''))
//{
preg_match("/^([0-9]{1})\/([0-9]{1})$/",$read,$ar);
//print_r($ar[1].'/'.$ar[2]);
print_r($ar);
echo("\n");
//}
fclose($file);
wyniki.txt
5/5
asd
4/5
fgh

Your regex seems ok. Perhaps you have some whitespace in the end of each line (you have used '$'). You can delete these by using trim(). Additionally, regex might not be the best solution in every case. Perhaps it would be simpler for you to test for the occurrence of '/', then use split() and check it the array[0] is a number (is_numeric()). Consider the following code:
$wyniki=file("wyniki.txt");
foreach($wyniki as $w) {
if (strpos($w, '/') !== FALSE) {
$tarr = split("\/", $w);
if (is_numeric($tarr[0]))
echo "This is a number: " . $tarr[0];
}
}
Second code with regex:
$flines = array("5/5","asd","4/5","fgh");
$regex = "#^([0-9]{1})\/([0-9]{1})$#";
$res = $tot = 0;
foreach ($flines as $fline) {
$arr = array();
preg_match($regex, $fline, $arr);
if (is_numeric($arr[1])) {
$res += $arr[1];
$tot++;
}
}
$avg = bcdiv($res, $tot, 2);
echo "Average: $avg";

Related

String between string with array in PHP, array order ISSUE

I'm facing an issue with a function that gets a string between two other strings.
function string_between($str, $starting_word, $ending_word) {
$subtring_start = strpos($str, $starting_word);
$subtring_start += strlen($starting_word);
foreach ($ending_word as $a){
$size = strpos($str, $a, $subtring_start) - $subtring_start;
}
return substr($str, $subtring_start, $size);
}
The issue is that the function searches for the first ending_word in the array.
An example will be easier to understand:
$array_a = ['the', 'amen']; // Starting strings
$array_b = [',', '.']; // Ending strings
$str = "Hello, the world. Then, it is over.";
Expected result:
"the world."
Current result:
"the world. Then,"
The function will think that the ending_word is "," because it is the first element met in the array_b. However, the text encounters first the '.' after the "the" starting word.
How can I make sure the function goes through the text and stops at the first element in the $str present in the array_b, whatever the position in the array?
Any idea?
Basically, you need to break outside of your foreach loop when $size > 0
That way it stops looping through your array when it finds the 1st occurrence. Here is the more complete code with other fixes:
function stringBetween($string, $startingWords, $endingWords) {
foreach ($startingWords as $startingWord) {
$subtringStart = strpos($string, $startingWord);
if ($subtringStart > 0) {
foreach ($endingWords as $endingWord){
$size = strpos($string, $endingWord, $subtringStart) - $subtringStart + strlen($endingWord);
if ($size > 0) {
break;
}
}
if ($size > 0) {
return substr($string, $subtringStart, $size);
}
}
}
return null;
}
$startArr = array('the', 'amen'); // Starting strings
$endArr = array('.', ','); // Ending strings
$str = "Hello, the world. Then, it is over.";
echo stringBetween($str, $startArr, $endArr); // the world.
This type of problems are best solved by PCRE regexes, only couple of lines needed in function :
function string_between($str, $starts, $ends) {
preg_match("/(?:{$starts}).*?(?:{$ends})/mi", $str, $m);
return $m[0];
}
Then calling like this :
echo string_between("Hello, the world. Then, it is over.", 'the|amen', ',|\.');
Produces : the world.
The trick,- search to the nearest matching ending symbol is done with regex non-greedy seach, indicated by question symbol in pattern .*?. You can even extend this function to accept arrays as starting/ending symbols, just that case modify function (possibly with implode('|',$arr)) for concatenating symbols into regex grouping formula.
Edited version
This works now. Iterate over your teststrings from first array looking for position of occurance from teststring. If found one then search for the second teststring at startposition from end of first string.
To get the shortest hit I store the position from the second and take the minimum.
You can try it at http://sandbox.onlinephpfunctions.com/code/0f1e5c97da62b4daaf0e49f52271fe288d1cacbb
$array_a =array('the','amen');
$array_b =array(',','.', '#');
$str = "Hello, the world. Then, it is over.";
function earchString($str, $array_a, $array_b) {
forEach($array_a as $test) {
$pos = strpos($str, $test);
if ($pos===false) continue;
$found = [];
forEach($array_b as $test2) {
$posStart = $pos+strlen($test);
$pos2 = strpos($str, $test2, $posStart);
$found[] = ($pos2!==false) ? $pos2 : INF;
}
$min = min($found);
if ($min !== INF)
return substr($str,$pos,$min-$pos) .$str[$min];
}
return '';
}
echo earchString($str, $array_a, $array_b);

Checking if a list of words exisit in a string

Hi I'm trying to check if a list of words (or any one of them) exists in a string. I tried some of the examples i found here, but i still can't get it to work correctly.
Any ideas what I'm doing wrong?
$ss3="How to make a book";
$words = array ("book","paper","page","sheet");
if (in_array($ss3, $words) )
{
echo "found it";
}
Loop over your array, check for each element if it exists in the string
$ss3="How to make a book";
$words = array ("book","paper","page","sheet");
foreach($words as $w){
if (stristr($ss3,$w)!==false)
echo "found $w \n";
}
Fiddle
You need to explode() your $ss3 string and then compare each item with your $words with loop
manual for in_array - http://php.net/manual/en/function.in-array.php
manual for explode() - http://php.net/manual/ru/function.explode.php
$matches = array();
$items = explode(" ",$ss3);
foreach($items as $item){
if(in_array($item, $words)){
$matches[] = $item; // Match found, storing in array
}
}
var_dump($matches); // To see all matches
This is how you will check for existence of a word from a string. Also remember that you must first convert the string to lowercase and then explode it.
$ss3="How to make a book";
$ss3 = strtolower($ss3);
$ss3 = explode(" ", $ss3);
$words = array ("book","paper","page","sheet");
if (in_array($ss3, $words) )
{
echo "found it";
}
Cheers!
You could use regular expressions. It would look something like this:
$ss3 = "How to make a book";
if (preg_match('/book/',$ss3))
echo 'found!!';
in_array will only check the complete string value in an array.
Now you can try this:
$string = 'How to make a book';
$words = array("book","paper","page","sheet");
foreach ($words as $val) {
if (strpos($string, $val) !== FALSE) {
echo "Match found";
return true;
}
}
echo "Not found!";
You can use str_word_count along with array_intersect like as
$ss3="How to make a book";
$words = array ("book","paper","page","sheet");
$new_str_array = str_word_count($ss3,1);
$founded_words = array_intersect($words,$new_str_array);
if(count($founded_words) > 0){
echo "Founded : ". implode(',',$founded_words);
}else{
echo "Founded Nothing";
}
Demo
This code will help you for better answer
<?php
$str="Hello World Good";
$word=array("Hello","Good");
$strArray=explode(" ",$str);
foreach($strArray as $val){
if(in_array($val,$word)){
echo $val."<br>";
}
}
?>

preg_match() match to cases with one pattern?

How would I go about ordering 1 array into 2 arrays depending on what each part of the array starts with using preg_match() ?
I know how to do this using 1 expression but I don't know how to use 2 expressions.
So far I can have done (don't ask why I'm not using strpos() - I need to use regex):
$gen = array(
'F1',
'BBC450',
'BBC566',
'F2',
'F31',
'SOMETHING123',
'SOMETHING456'
);
$f = array();
$bbc = array();
foreach($gen as $part) {
if(preg_match('/^F/', $part)) {
// Add to F array
array_push($f, $part);
} else if(preg_match('/^BBC/', $part)) {
// Add to BBC array
array_push($bbc, $part);
} else {
// Not F or BBC
}
}
So my question is: is it possible to do this using 1 preg_match() function?
Please ignore the SOMETHING part in the array, it's to show that using just one if else statement wouldn't solve this.
Thanks.
You can use an alternation along with the third argument to preg_match, which contains the part of the regexp that matched.
preg_match('/^(?:F|BBC)/', $part, $match);
switch ($match) {
case 'F':
$f[] = $part;
break;
case 'BBC':
$bbc[] = $part;
break;
default:
// Not F or BBC
}
It is even possible without any loop, switch, or anything else (which is faster and more efficient then the accepted answer's solution).
<?php
preg_match_all("/(?:(^F.*$)|(^BBC.*$))/m", implode(PHP_EOL, $gen), $matches);
$f = isset($matches[1]) ? $matches[1] : array();
$bbc = isset($matches[2]) ? $matches[2] : array();
You can find an interactive explanation of the regular expression at regex101.com which I created for you.
The (not desired) strpos approach is nearly five times faster.
<?php
$c = count($gen);
$f = $bbc = array();
for ($i = 0; $i < $c; ++$i) {
if (strpos($gen[$i], "F") === 0) {
$f[] = $gen[$i];
}
elseif (strpos($gen[$i], "BBC") === 0) {
$bbc[] = $gen[$i];
}
}
Regular expressions are nice, but the are no silver bullet for everything.

Retrieve word from string

I have this code:
$getClass = $params->get('pageclass_sfx');
var_dump($getClass); die();
The code above returns this:
string(24) "sl-articulo sl-categoria"
How can I retrieve the specific word I want without mattering its position?
Ive seen people use arrays for this but that would depend on the position (I think) that you enter these strings and these positions may vary.
For example:
$myvalue = $params->get('pageclass_sfx');
$arr = explode(' ',trim($myvalue));
echo $arr[0];
$arr[0] would return: sl-articulo
$arr[1] would return: sl-categoria
Thanks.
You can use substr for that in combination with strpos:
http://nl1.php.net/substr
http://nl1.php.net/strpos
$word = 'sl-categoria';
$page_class_sfx = $params->get('page_class_sfx');
if (false !== ($pos = strpos($page_class_sfx, $word))) {
// stupid because you already have the word... But this is what you request if I understand correctly
echo 'found: ' . substr($page_class_sfx, $pos, strlen($word));
}
Not sure if you want to get a word from the string if you already know the word... You want to know if it's there? false !== strpos($page_class_sfx, $word) would be enough.
If you know exactly what strings you're looking for, then stripos() should be sufficient (or strpos() if you need case-sensitivity). For example:
$myvalue = $params->get('pageclass_sfx');
$pos = stripos($myvalue, "sl-articulo");
if ($pos === FALSE) {
// string "sl-articulo" was not found
} else {
// string "sl-articulo" was found at character position $pos
}
If you need to check if some word are in string you may use preg_match function.
if (preg_match('/some-word/', 'many some-words')) {
echo 'some-word';
}
But this solution can be used for a small list of needed words.
For other cases i suggest you to use some of this.
$myvalue = $params->get('pageclass_sfx');
$arr = explode(' ',trim($myvalue));
$result = array();
foreach($arr as $key=> $value) {
// This will calculates all data in string.
if (!isset($result[$value])) {
$result[$value] = array(); // or 0 if you don`t need to use positions
}
$result[$value][] = $key; // For all positions
// $result[$value] ++; // For count of this word in string
}
// You can just test some words like follow:
if (isset($result['sl-categoria'])) {
var_dump($result['sl-categoria']);
}

Don't echo string if before string is similar to current string

I don't want to echo string if before string is similar to current string. Let's say our strings are,
$strings = array("software","software","game","antivirus");
My difference function,
function ($val1,$val2) {
similar_text($val1,$val2,$percent);
if ($percent>83) {
// should not echo. But don't know how to do.
}
}
But I don't know how can I do it. I guess it should be with using for each.
Try something like this:
$strings = array("software","software","game","antivirus");
$lastString = '';
foreach ($strings as $string) {
similar_text($lastString, $string, $percent);
if ($percent < 83) {
echo "$string<br />";
$lastString = $string;
}
}
If you don't understand some part of it, leave a comment and I will clarify.
Edit:
I moved the $lastString = $string; inside the condition.
Consider the following list of strings:
$strings = array("software","sofware","sofwart","ofwart","fwart","wart","warts");
Leaving the $lastString assignment outside of the loop would only print software even though lots of the words are very very different software they are not so different from the previous word.
Moving it inside actually gives the output :
software
sofwart
wart
$strings = array("software","software","game","antivirus");
$previous = '';
foreach ($strings as $string) {
if ($string===$previous) {
continue;
} else {
echo $string;
$previous = $string;
}
}
But I think it's better to do it with for like this (it should be faster):
$strings = array("software","software","game","antivirus");
$num = count($strings);
for ($i=0;$i<$num;$i++) {
if ($strings[$i]===$strings[$i-1] && $i!==0) {
continue;
} else {
echo $strings[$i];
}
}
Btw I totally did't get what the $percent means..
An approach using array_filter() (assumes >= 5.3):
$strings = array('software', 'software', 'game', 'antivirus');
$filtered = array_filter($strings, function($curr) {
static $prev;
similar_text($prev, $curr, $percent);
$prev = $curr;
if ($percent < 83) {
return $curr;
}
});
print_r($filtered);
Yields:
Array
(
[0] => software
[2] => game
[3] => antivirus
)
Hope this helps. Actually, I never knew about similar_text() until now. Pretty interesting function. Thanks :)

Categories