How to get values inside nested parenthesis expression [duplicate] - php

This question already has answers here:
Regex break string based on and/or in a search concept
(2 answers)
Closed 8 years ago.
I am trying to get the values within parenthesis in a given expression,
Example :
((1 or 2) and (3 and 4))
Output should be like this :
1 => 1 or 2
2 => 3 and 4
3 => 1 or 2 and 3 and 4
Please can anyone help me to get regexp to get the above result ?

((?<=\()[^)]+)
should do the job for you. But for the third result you will have to
substitute [()]
with nothing.
worth mentioning is the use of g modifier
demo here : http://regex101.com/r/eE7hU3

Related

REGEX with PHP (what am I doing wrong?) [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Let's say I want to place the bold content into a variable called $question_text.
1} What color is the sky:
A) Answer A
B) Answer B
C) Answer C
Answer: A
How do I use preg_match to do that?
I tried:
if(preg_match("#}(.*)#", $question, $question_match))
{
//Extract the question text
$question_text = trim($question_match[1]);
But it only gives me the first line: **What color is the sky: **
What am I doing wrong?
#(?s)}(.*)+?Answer: [A-Z]#
Worked for me!

Split 4 digit number into 2 variables in PHP [duplicate]

This question already has answers here:
split string after x characters
(3 answers)
Closed 7 months ago.
Using PHP...
If I have a number, for example, 0901, how can I then get 2 separate variables?
The first containing "09" and the second "01".
Thanks!
Using str_split. You can divide in half.
$number = "0901";
$arr = str_split($number, strlen($number)/2);
print $arr[0];
print $arr[1];

Generate random number with php must be 5 charachter... Even 1 = 00001 [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
My question subject can be seen easy, but i need something different.
$string = rand(1,99999) // echo 1
But my need it should
$string = rand(---?----) //echo 00001 - Always 5 characters..
For example when $string echo "23" it should write 00023 , if the $string writes 2 character left side will be 3times zero - 3 charachter, should write left side 2 times zero - 4 character , should write 1 time zero...
Logic is always 5 character...
I found the solution...
str_pad($string, 5, "0", STR_PAD_LEFT);

What does a single ampersand mean in a PHP conditional statement? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
The following code uses a single & in a conditional check. What does the single ampersand mean there?
if( $some_array_or_other_var & SOME_CONSTANT_VARIABLE ){
//do something here
}
It does not look like a reference, that's what confuses me.
That is a bitwise AND operation: http://www.php.net/manual/en/language.operators.bitwise.php
If, after the bitwise AND, the result is "truthy", the clause will be satisfied.
For example:
3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001
This is commonly used to check a single bit in a bitset, by testing powers of two, you are actually checking if a specific bit is set.

PHP: How does this statement work [duplicate]

This question already has answers here:
Strange echo, print behaviour in PHP?
(4 answers)
Closed 9 years ago.
How does this statement get evaluated to produce 234.
echo '3'.(print'2')+3; // output 234.
You are using the echo command to display a string. This string is a concatenation of a string "3" and the result of the call to print '2' added with the number 3.
During the concatenation print '2' is evaluated, thus the output starts with that. Afterwards the concatenated string '34' is printed, where the 4 is the result of 1 (=true, the result of the print call) + 3
Way too much text, but I hope that covers it all.

Categories