Regular expression php for a pro [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can you help me with a regular expression for this:
$value = '["number"]';
or
$value = '["number","number"]';
or
$value = '["number","number","number"]';
or
...
...
...
...
$value = '["number","number","number","number","number","number","number"......,"number"]';
//$number can be rand(0, 99.....999);
I need the result to be validated as true or false!

Here is my version:
function test($v)
{
if (preg_match('/^\\[("number")(,"number")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
or if "number" is really digits, this one:
function test($v)
{
if (preg_match('/^\\[("[0-9]+")(,"[0-9]+")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
NOTE - only positive naturals are accepted, need to change to negative and decimal/floating numbers

Do you really need to use PCRE? Your example is valid json.
$array = json_decode('["number", "number", "123"]');
var_dump($array);
If you need numbers only, you can filter it.
$new_array = array_filter($array, 'ctype_digit');
$result = count($array) == $new_array? $new_array : null;
var_dump($result);

Related

only higher values from array as variable number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have array with numbers and variable number:
$a = array(1,2,3,4,5,6,7,8,9,10);
$v = 5;
I need result all numbers if is higher as $v:
$result = ?? //array(6,7,8,9,10)
<?php
$a = array(1,2,3,4,5,6,7,8,9,10);
$v = 5;
foreach($a as $value){
if($value > $v ){
$new[] = $value;
}
}
echo "<pre>";
print_r($new);

rearrange positive and negative numbers php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
An array has negative and positive numbers. Separate the numbers such that negative numbers are at the beginning and positive numbers at the end without changing the order.
Example:
Array = {1, -3, -5, 9 , -8}
O/P = {-3, -5, -8, 1, 9}
I found many answer in c , c++ , java but not in PHP , so can any one please let me know how or better way to achieve this?
However this question put on hold , i tried with some of solutions and
find my own answer added below , hope it might be useful for someone.
function part($arr){
$j = 0;
for($i=0;$i<count($arr);$i++){
$val = $arr[$i];
$k = $i;
while($k>$j && $val < 0){
$arr[$k] = $arr[$k-1];
$k = $k-1;
if($j==$k){
$j=$j+1;
}
$arr[$k] = $val;
}
}
return $arr;
}
$arr = array(1, -3, -5, 9 , -8);
print_r(part($arr));
Without doing all the work for you.
Look into array_filter you could filter the negative values into 1 array, then filter the positive values into another array.
Then use array_merge to merge the 2.

How to find word position in sentence with string, array at php. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
please help me.
so how to find word position in sentece.
ex : one two five six seven two.
finding : two.
answer : 2 and 6
$needle = 'two';
$positions = array_keys(
array_filter(
str_word_count($sentence, 1),
function ($value) use ($needle) {
return $value == $needle;
}
)
);
EDIT
If you really need the offset of the first word to be 1 rather than 0, then modify the above so:
$positions = array_map(
function($position) {
return ++$position;
},
array_keys(
array_filter(
str_word_count($sentence, 1),
function ($value) use ($needle) {
return $value == $needle;
}
)
)
);

How to calculate currency string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to calculate two variables type "String" in php.
and I try all alternative but steady amount missing values.
example:
$a = "250,000.50";
$b = "30,000.00";
echo $a - $b;
I want to return value with same format ###,###,###.## -String -Decimal -only return same format.
Note: I try all the link format currency here...use regex, sprintf, replace, etc...
but always lose a digit because the example use in format en_US or EUR.
Thanks!.
You can use this function for easily handle:
$a = "250,000.50";
$b = "30,000.00";
function currency($c=0){
return preg_replace("/[^\d\.]+/iu","",$c);
}
echo number_format(currency($a) - currency($b) ,2);
The subtraction on strings will auto-cast the strings into numbers for you, but you have to get rid of the commas first:
$a = "250,000.50";
$b = "30,000.00";
echo str_replace(',', '', $a) - str_replace(',', '', $b);
Oh, and wrap the whole thing in number_format to get it back to the comma format:
echo number_format( str_replace(',', '', $a) - str_replace(',', '', $b), 2);

Array and string, exchange [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My code:
<?php
$string = "hello world";
echo $string[1]; //output is 'e'
echo end($string); // I've got a warning error
?>
How can I exchange my string variable to an array format?
I think if I exchange it the problem will solve.
$string[n] is a specific notation to access the nth offset of a string. This does not mean the string is an array, it's just special syntactic sugar. If you want the last offset of the string, use substr($string, -1).
There is a predefined function that do this. It's str_split(). Here http://it.php.net/manual/en/function.str-split.php you find the doc
Just do this:
echo substr($string, -1);
Because a string is not natively an array, functions like end and array_sort don't work.
To physically cast the string, use this:
function CastStringToArray($string)
{
$ret = array();
$length = strlen($string);
for ($i = 0; $i < $length; $i++)
{
$ret[] = $string[$i];
}
return $ret;
}

Categories