I am converting the following python into php
the aim is to remove scores from a string like "Liverpool 1 v 0 Everton"
home, away = event_data.get("desc").split(' v ')
# remove scores from event desc
if home.rsplit(' ', 1)[1].isdigit() and away.split(' ', 1)[0].isdigit():
event_name = home.rsplit(' ', 1)[0] + " v " + away.split(' ', 1)[1]
in php so far
$nameArray = explode(' v ', $value['name']);
$home = $nameArray[0];
$away = $nameArray[1];
$event_name = $home . ' v ' . $away;
im struggling with the stipping the scores, any tips?
Using preg_repalce in PHP you can replace digits around " v ":
$str = "Liverpool 1 v 0 Everton";
$event_name = preg_replace('/\h+\d+\h+v\h+\d+\h+/', ' v ', $str);
echo $event_name . "\n";
//=> Liverpool v Everton
Related
This code finds the two roots of a quadratic equation in the form ax^2+bx+c
Is my solution good time/space-complexity wise, and how I would go about allowing users to see the imaginary roots if the quadratic has any?
public function factor($a=0, $b=0, $c=0) {
$positive_solution = (-$b + sqrt($b**2-4*$a*$c))/2*$a;
$negative_solution = (-$b - sqrt($b**2-4*$a*$c))/2*$a;
if($b**2-4*$a*$c < 0) {
return "Solutions are imaginary";
}
$factor_one = $positive_solution * -1;
$factor_two = $negative_solution * -1;
$factor_one > 0 ? $factor_one = "+ " . $factor_one : $factor_one = "- " . $factor_one;
$factor_two > 0 ? $factor_two = "+ " . $factor_two : $factor_two = "- " . $factor_two;
return "Your roots are located at (0, " . $positive_solution . ")(0, " . $negative_solution . "),
Thus, the problem can be factored into (x " . $factor_one . ")(x " . $factor_two . ")";
}
the if($b**2-4*$a*$c < 0) is too late
You already used the sqrt so domain error might be thrown move the if to start
for imaginary part you just use negation or abs value
simply sqrt(-($b**2-4*$a*$c)) or sqrt(abs($b**2-4*$a*$c)) not sure if php uses abs or fabs for floats or if you even use floats... (havent code in php for ages)
I would combine this to something like this (just pseudo code as the var $ makes me dizzy :) ):
q = b*b-4*a*c
d = sqrt(abs(q))
if (q<0)
{
a0 = -b/(2*a);
b0 = +d/(2*a);
a1 = -b/(2*a);
b1 = -d/(2*a);
}
else
{
a0 = (-b + d)/(2*a);
b0 = 0
a1 = (-b - d)/(2*a);
b1 = 0
}
Where a0+i*b0 and a1+i*b1 are the 2 solutions where i=sqrt(-1)
I am trying to get the same MD5 hash in PHP and VB.NET but no matter what text encoding I use I cannot get the hashes to match. What am I doing wrong? Below is my code in each language:
PHP Code:
echo '<b>$20.00 (UTF-32)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32'));
echo '<b>$20.00 (UTF-32LE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32LE'));
echo '<b>$20.00 (UTF-32BE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32BE'));
echo '<b>$20.00 (UTF-8)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-8'));
echo '<b>$20.00 (UTF-7)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-7'));
echo '<b>$20.00 (UTF-16)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16'));
echo '<b>$20.00 (UTF-16LE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16LE'));
echo '<b>$20.00 (UTF-16BE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16BE'));
echo '<b>$20.00 (ASCII)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'ASCII'));
VB.NET Code:
Try
' Create a new instance of the MD5 object.
Dim md5Hasher As MD5 = MD5.Create()
' Convert the input string to a byte array and compute the hash.
Dim strInput As String = "1C9BRPS3TN85I2ULE5FP" & "20.00"
Dim data As Byte() = md5Hasher.ComputeHash(Encoding.UTF32.GetBytes(strInput))
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder.AppendFormat("{0:x2}", data(i))
'sBuilder.Append(data(i).ToString("x2"))
Next i
lblResponse.Text = "<b>$20.00 (UTF-32)</b> = " & sBuilder.ToString() & "<br>"
' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.ASCII.GetBytes(strInput))
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (ASCII)</b> = " & sBuilder.ToString() & "<br>"
' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(strInput))
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (Unicode)</b> = " & sBuilder.ToString() & "<br>"
' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.UTF7.GetBytes(strInput))
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (UTF7)</b> = " & sBuilder.ToString() & "<br>"
' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(strInput))
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (UTF8)</b> = " & sBuilder.ToString() & "<br>"
Catch ex As Exception
lblErrorMessage.Text = ex.ToString()
End Try
String concatenation is . in PHP not + as in VB.Net. Thus, '1C9BRPS3TN85I2ULE5FP' + '20.00' won't result in '1C9BRPS3TN85I2ULE5FP20.00'in PHP.
Edit: VB.net's Real string concatenation is & but since the term visual basic is used, + sign can do the same as long as mathematical interpretation is meaningless.
So to be on the safe side, use &.
<?php
$number1 = 1;
$number2 = 2;
echo $number1.' + ' . $number2. ' = '.$number1+$number2;
?>
See the above program. It is giving output as 3. Why not it is giving output as:
1 + 2 = 3
+ and . have the same precedence.
echo $number1.' + ' . $number2. ' = '.($number1+$number2);
The operations are applied in order. I.e:
echo $number1.' + ' . $number2. ' = '.$number1+$number2;
Becomes:
echo '1 + 2 = '.$number1+$number2;
Becomes:
echo '1 + 2 = 1'+$number2;
Since this is addition PHP will convert the string to an int which gives 1.
So the final expression is:
echo 1 + 2;//Prints 3
You can indicate which operations to perform together using brackets:
echo $number1.' + ' . $number2. ' = '.($number1+$number2);
your desired output :-
<?php
$number1 = 1;
$number2 = 2;
echo $number1.'+ ' . $number2.'='.($number1+$number2);
?>
I've a littile question about preg_matches, these regex things are really hard to understand and I hope someone can give the right awnser!
I have the following text:
0A-24-423
But this can also be:
0A-242-2
or
0A-2-423
How can I use preg_matches to filter these? I was using
substr($something, 0,2)
so that it captures 0A and
substr($seat, 4,5)
This will capture 24 but when you get 242 it wont capture the last 2....
Hope someone can help creating this in preg_match!
to make it more clear what I have now:
foreach($_POST['seats'] AS $seat) {
if ($count > 0) {
$selectQuery .= " || ";
}
$selectQuery .= " ( rowId = '" . substr($seat, 0,2) . "'";
$selectQuery .= " and `order` = " . substr($seat, 3,5) . " ";
$selectQuery .= " and columnId = " . substr($seat, 6) . " ) ";
$count++;
and $seat had the following format XXXXXX and using substr I can get the right things (for example: 0J3017)
Something Like this should do it:
$selectQuery = "SELECT * from seats where ";
$count = 0;
$pattern = "I DON'T KNOW :( ";
foreach($_POST['seats'] AS $seat) {
if ($count > 0) {
$selectQuery .= " || ";
}
preg_match($pattern, $seats, $matches);
$selectQuery .= " ( rowId = '" . $matches[0] . "'";
$selectQuery .= " and `order` = " . $matches[1] . " ";
$selectQuery .= " and columnId = " . $matches[2] . " ) ";
$count++;
and $seats is explained in the beginning of the post (it has a format of XX-XXX-XXX
where the first 2 XX are 0[A-Z] (yes the 0 is correct)
where the 3 first XXX are [0-9]
Where the last 3 XXX are [0-9]
EDIT:
there are 2 ways to solve this.
Option 1:
$pattern = "/(.*)-(.*)-(.*)/";
or use explode() function.
It does not look like you need to be using regular expressions. Here's an example using explode() and list():
list($row_id, $order, $column_id) = explode('-', $seat, 3);
You could then use those three new variables in your $selectQuery.
EDIT : Since the OP has stated his requirement as a comment to my answer I have updated my answer accordingly.
You can try this:
$pattern = "/[A-Z\d]{1,2}-[A-Z\d]{1,3}-[A-Z\d]{1,3}/";
$matched = preg_match($pattern, $something);
if ($matched === 0) {
die('regex did not match');
}
$matched will give you 1 for a matched string and 0 if not matched.
I want to bring the last three words of a string to its beginning. For example, these two variables:
$vari1 = "It is a wonderful goodbye letter that ultimately had a happy ending.";
$vari2 = "The Science Museum in London is a free museum packed with interactive exhibits.";
Should become:
"A happy ending - It is a wonderful goodbye letter that ultimately had."
"With interactive exhibits - The Science Museum in London is a free museum packed."
Exploding, rearranging, and then imploding should work. See the example here
$array = explode(" ", substr($input_string,0,-1));
array_push($array, "-");
for($i=0;$i<4;$i++)
array_unshift($array, array_pop($array));
$output_string = ucfirst(implode(" ", $array)) . ".";
$split = explode(" ",$vari1);
$last = array_pop($split);
$last = preg_replace("/\W$/","",$last);
$sec = array_pop($split);
$first = array_pop($split);
$new = implode(" ",array(ucfirst($first),$sec,$last)) . " - " . implode(" ",$split) . ".";
Or similar should do the trick.
Make sure to love me tender. <3
function switcheroo($sentence) {
$words = explode(" ",$sentence);
$new_start = "";
for ($i = 0; $i < 3; $i++) {
$new_start = array_pop($words)." ".$new_start;
}
return ucfirst(str_replace(".","",$new_start))." - ".implode(" ",$words).".";
}
This will get you the exact same output that you want in only 2 lines of code
$array = explode(' ', $vari1);
echo ucfirst(str_replace('.', ' - ', join(' ', array_merge(array_reverse(array(array_pop($array), array_pop($array), array_pop($array))), $array)))) . '.';