Php If with || not working - php

do {
if ($dir[$i] == " " || is_numeric($dir[$i])
{
$direccion=$direccion.$dir[$i];
}
elseif ($i>6)
{
$i=strlen($dir)
}
}
while ($i <= count($dir))
$dir is the variable i use to get a google address, but the thing is that it comes with Zip code and City. The thing is that I only need the Street and number.
in the first run $dir = "Calle 30 433B29938 LaPlataBuenosAiresArgentina"
And i only need from that 30 433
Since the word " Calle " is always in the return, I've made an if that if its > 6, $i automatically will equal the lenght of the chain and kick me out of the while, because I'll be in "Calle " and there's when the 30 433 begins. The other ifs that I've made, one of them controls if It's a space to add it to the new string $direccion, and the other one checks if It's a number. If it's not a number and if It's not a space and $i > 6, means that I'm in B and I want to finish the do/while.
result of $direccion should be = 30 433. But i get an error on line 3 telling me that there is a problem with a {.

first thing you forget a ) at the end of your if condition
if ($dir[$i] == " " || is_numeric($dir[$i]))
and second if you want to get number out of a string you can use
preg_match_all('!\d+!', $str, $matches);
like if i use this code
$str = 'Calle 30 433B29938 LaPlataBuenosAiresArgentina ';
preg_match_all('!\d+!', $str, $matches);
output will be
Array ( [0] => Array ( [0] => 30 [1] => 433 [2] => 29938 ) )
But only if you are sure that your string will be exact you defined.
i am not sure but hope it can help.

Related

PHP program to accept user input and terminate when entered zero

I came across this program on http://www.programmr.com . And the question is
Complete the program to print the sum of negative numbers, positive even numbers, positive odd numbers from a list of numbers entered by the user. The list terminates when the number entered is zero . And my code is,
$nums = array();
while(trim(fgets(STDIN)) != 0){
array_push($nums,trim(fgets(STDIN)));
}
I know the code is incomplete but what im trying to do is to push the inputs to the array and then calculate the sum. When I print_r($nums) the array it gives me this,
Array
(
[0] => 34
[1] => 32
[2] => 45
[3] => 0
)
And my input is,
12
34
12
32
12
45
12
0
0
It pushes the alternative elements i dont know whats happening with this. Please help me , thanks in advance.
You are calling fgets(STDIN) twice in your code, i have adjusted it a bit so the array part is working. The rest of the assignment i let you figure that part out ;) Hint: Use modulus operator.
$nums = array();
do {
$number = (int) trim(fgets(STDIN));
array_push($nums,$number);
} while ($number !== 0);
print_r($nums);
Also if you are using PHP5.6 or higher you can use short array syntax like so:
$nums = [];
And
$nums[] = $number;

PHP Getting Scores From A File

I have an htm file that holds scores for a game. I need to find each score above a 65, insert those scores and names only into a db and then print the entire file on the page with the high score lines in red. The htm file includes simple text:
<pre>
Thursday Afternoon Pairs Thursday Aft Session February 1, 2018
Scores after 5 rounds Average: 50.0 Section A North-South
Pair Pct Score Section Rank MPs
A B C
5 65.00 65.00 A 1 0.50(A) Joseph - Marlene
1 47.00 47.00 B 2/3 1 0.30(A) Janet - Mina
3 47.00 47.00 A 2/3 0.30(A) Renee - Reichle
</pre>
There is no other tags in the file and the file cannot be modified.
I've only gotten as far as trying to find the highscores and see if I can get those to print. This returns no matches found everytime.
$file = 108911.htm;
$pct = "([1-9][0-9]\.[0-9]{2})";
$highgame_pct = 65;
$contents = file_get_contents($file);
$pattern = preg_quote($pct, '/');
$pattern = "/^.*$pct.*\$/m";
if(preg_match_all($pattern, $contents, $matches) >= $highgame_pct){
echo "Found matches:<br />";
echo implode("<br />\n", $matches[0]);
}
else{
echo "No matches found";
}
preg_match_all returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
Using similar regex pattern and using callback function to filter array:
# define array filter callback function
function filterArray($value1){
return ($value1 >= 65.00);
}
# read file
$file = "108911.htm";
$contents = file_get_contents($file);
# define regex
$pattern = "/(\d{2}\.\d{2})/m";
# find matches
$val = preg_match_all($pattern, $contents, $matches);
echo "Count of matches = $val\n";
# filter matches array
$filteredArray = array_filter($matches[0], 'filterArray');
# print matched element
if (count($filteredArray) > 0) {
echo "Found matches bigger or equal to 65.00:<br />";
echo implode("<br />\n", $filteredArray);
}
else{
echo "No matches found";
}
Output:
Count of matches = 10
Found matches bigger or equal to 65.00:<br />
65.00<br />
65.00
You still need to modify this to match the "Score" column only.
Like anything, there are a lots of ways to do this, here is one to try:
# Set the threshold
$max = 65;
# This will fetch file to array, trim spaces, then remove empty
# using file() will take the file and automatically turn it to an array
$arr = array_filter(file('108911.htm',FILE_SKIP_EMPTY_LINES),function($v){
return (!empty(trim($v)));
});
# This will iterate and find matched values based on pattern
$file = array_values(array_filter(array_map(function($v) use ($max){
# Find values that match decimal pattern (allows 1 to 3 whole numbers and up to 2 decimal points)
preg_match_all('/[0-9]{1,3}\.[0-9]{1,2}/',$v,$match);
# Checks if there are at least 3 values in matched array
$match = (!empty($match[0]) && count($match[0]) == 3)? $match[0] : false;
# If not, just return empty
if(empty($match))
return false;
# If the second value (Points) is less than the max, return empty
if($match[1] < $max)
return false;
# Get the name value
preg_match('/[\w]+ - [\w]+/',$v,$name);
# Return the points and the name
return [
'points' => $match[1],
'name' => $name[0]
];
},$arr)));
# Here is the raw array. You can loop this now to get the rows to insert
print_r($file);
Gives you:
Array
(
[0] => Array
(
[points] => 65.00
[name] => Joseph - Marlene
)
)

Trim symbol and decimal value from currency string

I have currency input strings like these.
$50 ...I only need 50
$60.59 ...I only need 60, need to remove $ and .59
€360 ...I only need 360
€36.99 ...I only need 36, need to remove € and .99
£900 ...I only need 900
£90.99 ...I only need 90
In other words, I need to remove all currency symbols from the start of the string and I only need the integer value -- the decimal values can be cut off.
This RegEx should do it
(\$|€|£)\d+
This is even better (thanks to Jan)
[$€£]\d+
Use it with PHP's Preg Match
preg_match — Perform a regular expression match
I would recommend not using a regular expression, as it's overkill for this scenario.
$str = (int)ltrim($str, '$£€');
this is all you need.
Performance vs Regex
I ran the above test through a script to see what the time difference is between my answer and using a RegEx, and on average the RegEx solution was ~20% slower.
<?php
function funcA($a) {
echo (int)ltrim($a, '$£€');
};
function funcB($a) {
echo preg_replace('/^.*?([0-9]+).*$/', '$1', $a);
};
//setup (only run once):
function changeDataA() {}
function changeDataB() {}
$loops = 50000;
$timeA = 0.0;
$timeB = 0.0;
$prefix = str_split('€$€');
ob_start();
for($i=0; $i<$loops; ++$i) {
$a = $prefix[rand(0,2)] . rand(1,999) . '.' . rand(10,99);
$start = microtime(1);
funcA($a);
$timeA += microtime(1) - $start;
$start = microtime(1);
funcB($a);
$timeB += microtime(1) - $start;
}
ob_end_clean();
$timeA = round(1000000 * ($timeA / $loops), 3);
$timeB = round(1000000 * ($timeB / $loops), 3);
echo "
TimeA averaged $timeA microseconds
TimeB averaged $timeB microseconds
";
Timings vary depending on system load, so times should be considered only relative to each other, not compared between executions. Also this isn't a perfect script for performance benchmarking, there are outside influences that can affect these results, but this gives a general idea.
TimeA averaged 5.976 microseconds
TimeB averaged 6.831 microseconds
Use regular expression. Ex:
$toStr = preg_replace('/^.*?([0-9]+).*$/', '$1', $fromStr);
See preg_replace documentation.
use below way
$str = '$50 From here i need only 50
$60.59 From here i need only 60, Need to remove $ and .59
€360 From here i need only 360.
€36.99 From here i need only 36 need to remove € and .99.
£900 From here i need only 900.
£90.99 From here i need only 90.';
$arr_ = array('$','€','£');
echo str_replace($arr_,'',$str);
You could go for:
<?php
$string = <<<DATA
$50 From here i need only 50
$60.59 From here i need only 60, Need to remove $ and .59
€360 From here i need only 360.
€36.99 From here i need only 36 need to remove € and .99.
£900 From here i need only 900.
£90.99 From here i need only 90.
DATA;
# look for one of $,€,£ followed by digits
$regex = '~[$€£]\K\d+~';
preg_match_all($regex, $string, $amounts);
print_r($amounts);
/*
Array
(
[0] => Array
(
[0] => 50
[1] => 60
[2] => 360
[3] => 36
[4] => 900
[5] => 90
)
)
*/
?>
See a demo on ideone.com.
$newString=$string;
$currencyArray = array("$","€","£"); //just add the new item if you want that to add more
foreach($currencyArray as $value)
$newString= str_replace($value,"",$newString);
$newString has what you need.

Php: String indexing inconsistant?

I have created a function which randomly generates a phrase from a hardcoded list of words. I have a function get_words() which has a string of hardcoded words, which it turns into an array then shuffles and returns.
get_words() is called by generate_random_phrase(), which iterates through get_words() n times, and on every iteration concatenates the n word into the final phrase which is destined to be returned to the user.
My problem is, for some reason PHP keeps giving me inconsistent results. It does give me words which are randomized, but it gives inconsistent number of words. I specify 4 words as the default and it gives me phrases ranging from 1-4 words instead of 4. This program is so simple it is almost unbelievable I can't pinpoint the exact issue. It seems like the broken link in the chain is the $words array which is being indexed, it seems like for some reason sometimes the indexing fails. I am unfamiliar with PHP, can someone explain this to me?
<?php
function generate_random_phrase() {
$words = get_words();
$number_of_words = get_word_count();
$phrase = "";
$symbols = "!##$%^&*()";
echo print_r($phrase);
for ($i = 0;$i < $number_of_words;$i++) {
$phrase .= " ".$words[$i];
}
if (isset($_POST['include_numbers']))
$phrase = $phrase.rand(0, 9);
if (isset($_POST['include_symbols']))
$phrase = $phrase.$symbols[rand(0, 9)];
return $phrase;
}
function get_word_count() {
if ($_POST['word_count'] < 1 || $_POST['word_count'] > 9)
$word_count = 4; #default
else
$word_count = $_POST['word_count'];
return $word_count;
}
function get_words() {
$BASE_WORDS = "my sentence really hope you
like narwhales bacon at midnight but only
ferver where can paper laptops spoon door knobs
head phones watches barbeque not say";
$words = explode(' ', $BASE_WORDS);
shuffle($words);
return $words;
}
?>
In $BASE_WORDS your tabs and new lines are occupying a space in the exploded array that's why. Remove the newlines and tabs and it'll generate the correct answer. Ie:
$BASE_WORDS = "my sentence really hope you like narwhales bacon at midnight but only ferver where can paper laptops spoon door knobs head phones watches barbeque not say";
Your function seems a bit inconsistent since you also include spaces inside the array, thats why when you included them, you include them in your loop, which seems to be 5 words (4 real words with one space index) is not really correct. You could just filter spaces also first, including whitespaces.
Here is the visual representation of what I mean:
Array
(
[0] => // hello im a whitespace, i should not be in here since im not really a word
[1] => but
[2] =>
[3] => bacon
[4] => spoon
[5] => head
[6] => barbeque
[7] =>
[8] =>
[9] => sentence
[10] => door
[11] => you
[12] =>
[13] => watches
[14] => really
[15] => midnight
[16] =>
So when you loop it, you include spaces, in this case. If you got a number of words of 5, you really dont get those 5 words, index 0 - 4 it will look like you only got 3 (1 => but, 3 => bacon, 4 => spoon).
Here is a modified version:
function generate_random_phrase() {
$words = get_words();
$number_of_words = get_word_count();
$phrase = "";
$symbols = "!##$%^&*()";
$words = array_filter(array_map('trim', $words)); // filter empty words
$phrase = implode(' ', array_slice($words, 0, $number_of_words)); // no need for a loop
// this simply gets the array from the first until the desired number of words (0,5 or 0,9 whatever)
// and then implode, just glues all the words with space
// so this ensure its always according to how many words you want
if (isset($_POST['include_numbers']))
$phrase = $phrase.rand(0, 9);
if (isset($_POST['include_symbols']))
$phrase = $phrase.$symbols[rand(0, 9)];
return $phrase;
}
Inconsistent spacing in your words list is the issue.
Here is a fix:
function get_words() {
$BASE_WORDS = "my|sentence|really|hope|you|
|like|narwhales|bacon|at|midnight|but|only|
|ferver|where|can|paper|laptops|spoon|door|knobs|
|head|phones|watches|barbeque|not|say";
$words = explode('|', $BASE_WORDS);
shuffle($words);
return $words;
}

PHP Strip numbers

Hello I need to put dot between first number and second number my result is:
11 I need to make him like this 1.1 how ?
if ( strlen($model->rate) == 2 )
{
}
$model->rate = $model->rate[0] . '.' . $model->rate[1];
Also, your if condition should look like the following (note the number of equal signs):
if ( strlen($model->rate) == 2 )

Categories