Related
I have this sample JSON file:
{
"users":[
{"username":"user123","password":"123321abc"},
{"username":"user124","password":"123321abc"},
{"username":"user125","password":"123321abc"}
]
}
Im setting it like that becouse i need to read it in php with fgets. Like this:
<?php
$newUser = ["username"=>"user123","password"=>"123321abc"];
$jsonFile = fopen("JSONsample.json","r+");
while(($line = fgets($jsonFile) != false){
$oldUser = json_decode($line,true);
if($oldUser["username"] === newUser["username"]){
if($newUser["password"] === oldUSer["password"]){
doSomething($newUser);
}
}
?>
The thing is when fgets read lines that are not a complete json object it returns null. So it will read the first two user objects as null becouse of the comma at the end of the line. I could just erase the comma and it would return the user as I need it to. But it wouldn't be a valid JSON as a whole. I need the "users" array for other functions.
So, my question would be is there any way I can arrange my JSON so its a valid JSON file when taken as a whole and when its read line by line?
make sure each line starts with a curly bracket '{' and ends with a curly bracket '}' in your case. Then you can assume it is a valid JSON then apply json_decode
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
$newUser = ["username"=>"user123","password"=>"123321abc"];
$jsonFile = fopen("JSONsample.json","r+");
while(($line = fgets($jsonFile) != false){
$line = trim($line);
if(!startswith($line,"{") || !endswith($line,"}")) continue;
$oldUser = json_decode($line,true);
if($oldUser["username"] === newUser["username"] && $newUser["password"] === oldUSer["password"]){
doSomething($newUser);
}
}
$json_file_data = json_decode(file_get_contents('JSONsample.json'), true);
$users_info = $json_file_data.users;
$newUser = $users_info[0];
$oldUser = $users_info[1];
I guess you can get the data from here easily, let me know if I could help ypu anyway.
There's composer packages out there that solves memory issues by parsing file streams instead of reading it all into memory, like for example the package
salsify/json-streaming-parser:
At Packagist:
https://packagist.org/packages/salsify/json-streaming-parser
On GitHub:
https://github.com/salsify/jsonstreamingparser
We have got web app which does replacing some text with another using str_replace().
Find strings and replace strings are stored in template file.
We what to replace str_replace() function to preg_replace() to have possibility to use regex in find strings (to set them in the same template file).
In original scripts we have such parts of php code.
In one file:
class SiteConfig {
// Strings to search for in HTML before processing begins (used with $replace_string)
public $find_string = array();
// Strings to replace those found in $find_string before HTML processing begins
public $replace_string = array();
// a lot of code goes here
public function append(SiteConfig $newconfig) {
foreach (array('find_string', 'replace_string') as $var) {
// append array elements for this config variable from $newconfig to this config
//$this->$var = $this->$var + $newconfig->$var;
$this->$var = array_merge($this->$var, $newconfig->$var);
}
}
// a lot of code goes here
public static function build_from_array(array $lines) {
$config = new SiteConfig();
foreach ($lines as $line) {
$line = trim($line);
// skip comments, empty lines
if ($line == '' || $line[0] == '#') continue;
// get command
$command = explode(':', $line, 2);
// if there's no colon ':', skip this line
if (count($command) != 2) continue;
$val = trim($command[1]);
$command = trim($command[0]);
//if ($command == '' || $val == '') continue;
// $val can be empty, e.g. replace_string:
if ($command == '') continue;
// strip_attr is now an alias for strip.
// In FTR 3.8 we can strip attributes from elements, not only the elements themselves
// e.g. strip: //img/#srcset (removes srcset attribute from all img elements)
// but for backward compatibility (to avoid errors with new config files + old version of FTR)
// we've introduced strip_attr and we'll recommend using that in our public site config rep.
// strip_attr: //img/#srcset
if ($command == 'strip_attr') $command = 'strip';
// check for commands where we accept multiple statements
if (in_array($command, array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'native_ad_clue', 'http_header', 'test_url', 'find_string', 'replace_string'))) {
array_push($config->$command, $val);
// check for single statement commands that evaluate to true or false
} elseif (in_array($command, array('tidy', 'prune', 'autodetect_on_failure', 'insert_detected_image'))) {
$config->$command = ($val == 'yes');
// check for single statement commands stored as strings
} elseif (in_array($command, array('parser'))) {
$config->$command = $val;
// special treatment for test_contains
} elseif (in_array($command, array('test_contains'))) {
$config->add_test_contains($val);
// special treatment for if_page_contains
} elseif (in_array($command, array('if_page_contains'))) {
$config->add_if_page_contains_condition($val);
// check for replace_string(find): replace
} elseif ((substr($command, -1) == ')') && preg_match('!^([a-z0-9_]+)\((.*?)\)$!i', $command, $match)) {
if (in_array($match[1], array('replace_string'))) {
array_push($config->find_string, $match[2]);
array_push($config->replace_string, $val);
} elseif (in_array($match[1], array('http_header'))) {
$_header = strtolower(trim($match[2]));
$config->http_header[$_header] = $val;
}
}
}
return $config;
}
}
In another file:
public function process($html, $url, $smart_tidy=true) {
// a lot of code goes before
// do string replacements
if (!empty($this->config->find_string)) {
if (count($this->config->find_string) == count($this->config->replace_string)) {
$html = str_replace($this->config->find_string, $this->config->replace_string, $html, $_count);
$this->debug("Strings replaced: $_count (find_string and/or replace_string)");
} else {
$this->debug('Skipped string replacement - incorrect number of find-replace strings in site config');
}
unset($_count);
}
// a lot of code goes after
}
I tried to replace str_replace() with preg_replace(), but while testing it shows an error:
Warning: preg_replace(): No ending matching delimiter '>' found in this line:
$html = preg_replace($this->config->find_string, $this->config->replace_string, $html, $_count);
Where is the error and how to replace str_replace() function to preg_replace() correctly?
I'm very very beginning in php, so any help is badly needed.
Big thanks in advance!
Rewrite your process function like this:
public function process($html, $url, $smart_tidy=true) {
// a lot of code goes before
// do string replacements
if (!empty($this->config->find_string)) {
if (count($this->config->find_string) == count($this->config->replace_string)) {
$new_config_find_string = array_map(function($new_pattern)
{
return '/'.preg_quote($new_pattern).'/';
},$this->config->find_string);
$html = preg_replace($new_config_find_string, $this->config->replace_string, $html, $_count);
$this->debug("Strings replaced: $_count (find_string and/or replace_string)");
} else {
$this->debug('Skipped string replacement - incorrect number of find-replace strings in site config');
}
unset($_count);
}
// a lot of code goes after
}
I have urls such as
/blah/WHATEVER/sites/two/one/blah/need/this.ini
in PHP, how do I extract /need/this.ini with regular expressions?
Without:
$url = '/blah/WHATEVER/sites/two/one/blah/need/this.ini';
$array = explode('/',$url);
$rev = array_reverse($array);
$last = $rev[0];
$second_last = $rev[1];
// or $rev[1].'/'.$rev[0]
A bit longer, I'm sure there are even better and clearer ways than this. Anyway, just to say you don't need regexes for this kind of stuff. Regexes are not a solution for everything :)
If you don't need the array intact, you can also array_pop() twice, and each time you get the last element. But you will shorten the array by one element each time.
Also:
$url = '/blah/WHATEVER/sites/two/one/blah/need/this.ini';
$array = explode('/',$url);
$last = end($array);
$second_last = prev($array);
See it in action
This should do:
(/[^/]+/[^/]+)$
(does not check for escaped slashes though.)
Notice # instead of /
if (preg_match('#/?[^/]+?/?[^/]+$#', $path, $m) !== false) {
// $m[0]` will contain the desired part
}
But there is better way to do this - don't use regexp at all:
function extract_part($path) {
$pos = strrpos( $path, '/');
if ($pos > 0) { // try to find the second one
$npath = substr($path, 0, $pos-1);
$npos = strrpos($npath, '/');
if ($npos !== false) {
return substr($path, $npos);
}
// This is not error, uncomment to change code behaviour.
/*else { // Returns '/this.ini' in your case
return substr($path, $pos);
}*/
}
// Returns as is
return $path;
}
(I have no php interpreter under my hands, so the code is not checked).
Yep, there was an error :) and now its fixed.
I was just wondering if PHP has a function that can take a string like 2-1 and produce the arithmetic result of it?
Or will I have to do this manually with explode() to get the values left and right of the arithmetic operator?
I know this question is old, but I came across it last night while searching for something that wasn't quite related, and every single answer here is bad. Not just bad, very bad. The examples I give here will be from a class that I created back in 2005 and spent the past few hours updating for PHP5 because of this question. Other systems do exist, and were around before this question was posted, so it baffles me why every answer here tells you to use eval, when the caution from PHP is:
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
Before I jump in to the example, the places to get the class I will be using is on either PHPClasses or GitHub. Both the eos.class.php and stack.class.php are required, but can be combined in to the same file.
The reason for using a class like this is that it includes and infix to postfix(RPN) parser, and then an RPN Solver. With these, you never have to use the eval function and open your system up to vulnerabilities. Once you have the classes, the following code is all that is needed to solve a simple (to more complex) equation such as your 2-1 example.
require_once "eos.class.php";
$equation = "2-1";
$eq = new eqEOS();
$result = $eq->solveIF($equation);
That's it! You can use a parser like this for most equations, however complicated and nested without ever having to resort to the 'evil eval'.
Because I really don't want this only only to have my class in it, here are some other options. I am just familiar with my own since I've been using it for 8 years. ^^
Wolfram|Alpha API
Sage
A fairly bad parser
phpdicecalc
Not quite sure what happened to others that I had found previously - came across another one on GitHub before as well, unfortunately I didn't bookmark it, but it was related to large float operations that included a parser as well.
Anyways, I wanted to make sure an answer to solving equations in PHP on here wasn't pointing all future searchers to eval as this was at the top of a google search. ^^
$operation='2-1';
eval("\$value = \"$operation\";");
or
$value=eval("return ($operation);");
This is one of the cases where eval comes in handy:
$expression = '2 - 1';
eval( '$result = (' . $expression . ');' );
echo $result;
You can use BC Math arbitrary precision
echo bcsub(5, 4); // 1
echo bcsub(1.234, 5); // 3
echo bcsub(1.234, 5, 4); // -3.7660
http://www.php.net/manual/en/function.bcsub.php
In this forum someone made it without eval. Maybe you can try it? Credits to them, I just found it.
function calculate_string( $mathString ) {
$mathString = trim($mathString); // trim white spaces
$mathString = ereg_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString); // remove any non-numbers chars; exception for math operators
$compute = create_function("", "return (" . $mathString . ");" );
return 0 + $compute();
}
$string = " (1 + 1) * (2 + 2)";
echo calculate_string($string); // outputs 8
Also see this answer here: Evaluating a string of simple mathematical expressions
Please note this solution does NOT conform to BODMAS, but you can use brackets in your evaluation string to overcome this.
function callback1($m) {
return string_to_math($m[1]);
}
function callback2($n,$m) {
$o=$m[0];
$m[0]=' ';
return $o=='+' ? $n+$m : ($o=='-' ? $n-$m : ($o=='*' ? $n*$m : $n/$m));
}
function string_to_math($s){
while ($s != ($t = preg_replace_callback('/\(([^()]*)\)/','callback1',$s))) $s=$t;
preg_match_all('![-+/*].*?[\d.]+!', "+$s", $m);
return array_reduce($m[0], 'callback2');
}
echo string_to_match('2-1'); //returns 1
As create_function got deprecated and I was utterly needed an alternative lightweight solution of evaluating string as math. After a couple of hours spending, I came up with following. By the way, I did not care about parentheses as I don't need in my case. I just needed something that conform operator precedence correctly.
Update: I have added parentheses support as well. Please check this project Evaluate Math String
function evalAsMath($str) {
$error = false;
$div_mul = false;
$add_sub = false;
$result = 0;
$str = preg_replace('/[^\d\.\+\-\*\/]/i','',$str);
$str = rtrim(trim($str, '/*+'),'-');
if ((strpos($str, '/') !== false || strpos($str, '*') !== false)) {
$div_mul = true;
$operators = array('*','/');
while(!$error && $operators) {
$operator = array_pop($operators);
while($operator && strpos($str, $operator) !== false) {
if ($error) {
break;
}
$regex = '/([\d\.]+)\\'.$operator.'(\-?[\d\.]+)/';
preg_match($regex, $str, $matches);
if (isset($matches[1]) && isset($matches[2])) {
if ($operator=='+') $result = (float)$matches[1] + (float)$matches[2];
if ($operator=='-') $result = (float)$matches[1] - (float)$matches[2];
if ($operator=='*') $result = (float)$matches[1] * (float)$matches[2];
if ($operator=='/') {
if ((float)$matches[2]) {
$result = (float)$matches[1] / (float)$matches[2];
} else {
$error = true;
}
}
$str = preg_replace($regex, $result, $str, 1);
$str = str_replace(array('++','--','-+','+-'), array('+','+','-','-'), $str);
} else {
$error = true;
}
}
}
}
if (!$error && (strpos($str, '+') !== false || strpos($str, '-') !== false)) {
$add_sub = true;
preg_match_all('/([\d\.]+|[\+\-])/', $str, $matches);
if (isset($matches[0])) {
$result = 0;
$operator = '+';
$tokens = $matches[0];
$count = count($tokens);
for ($i=0; $i < $count; $i++) {
if ($tokens[$i] == '+' || $tokens[$i] == '-') {
$operator = $tokens[$i];
} else {
$result = ($operator == '+') ? ($result + (float)$tokens[$i]) : ($result - (float)$tokens[$i]);
}
}
}
}
if (!$error && !$div_mul && !$add_sub) {
$result = (float)$str;
}
return $error ? 0 : $result;
}
Demo: http://sandbox.onlinephpfunctions.com/code/fdffa9652b748ac8c6887d91f9b10fe62366c650
Here is a somewhat verbose bit of code I rolled for another SO question. It does conform to BOMDAS without eval(), but is not equipped to do complex/higher-order/parenthetical expressions. This library-free approach pulls the expression apart and systematically reduces the array of components until all of the operators are removed. It certainly works for your sample expression: 2-1 ;)
preg_match() checks that each operator has a numeric substring on each side.
preg_split() divides the string into an array of alternating numbers and operators.
array_search() finds the index of the targeted operator, while it exists in the array.
array_splice() replaces the operator element and the elements on either side of it with a new element that contains the mathematical result of the three elements removed.
** updated to allow negative numbers **
Code: (Demo)
$expression = "-11+3*1*4/-6-12";
if (!preg_match('~^-?\d*\.?\d+([*/+-]-?\d*\.?\d+)*$~', $expression)) {
echo "invalid expression";
} else {
$components = preg_split('~(?<=\d)([*/+-])~', $expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
var_export($components); // ['-11','+','3','*','1','*','4','/','-6','-','12']
while (($index = array_search('*',$components)) !== false) {
array_splice($components, $index - 1, 3, $components[$index - 1] * $components[$index + 1]);
var_export($components);
// ['-11','+','3','*','4','/','-6','-','12']
// ['-11','+','12','/','-6','-','12']
}
while (($index = array_search('/', $components)) !== false) {
array_splice($components, $index - 1, 3, $components[$index - 1] / $components[$index + 1]);
var_export($components); // [-'11','+','-2','-','12']
}
while (($index = array_search('+', $components)) !== false) {
array_splice($components, $index - 1, 3, $components[$index - 1] + $components[$index + 1]);
var_export($components); // ['-13','-','12']
}
while (($index = array_search('-', $components)) !== false) {
array_splice($components, $index - 1, 3, $components[$index - 1] - $components[$index + 1]);
var_export($components); // [-25]
}
echo current($components); // -25
}
Here is a demo of the BOMDAS version that uses php's pow() when ^ is encountered between two numbers (positive or negative).
I don't think I'll ever bother writing a version that handles parenthetical expressions ... but we'll see how bored I get.
You can do it by eval function.
Here is how you can do this.
<?php
$exp = "2-1;";
$res = eval("return $exp");
echo $res; // it will return 1
?>
You can use $res anywhere in the code to get the result.
You can use it in form by changing $exp value.
Here is an example of creating a web calculator.
<?php
if (isset($_POST['submit'])) {
$exp = $_POST['calc'];
$res = eval("return $exp;"); //Here we have to add ; after $exp to make a complete code.
echo $res;
}
?>
// html code
<form method="post">
<input type="text" name="calc">
<input type="submit" name="submit">
</form>
I have a viariable that contains something like this ab_123456789
how can i check if the variable starts with ab_?
thanks,
sebastian
Another way using substr:
if (substr('ab_123456789', 0, 3) === 'ab_')
Here substr is used to take the first 3 bytes starting at position 0 as a string that is then compared to 'ab_'. If you want to add case-insensivity, use strcasecmp.
Edit To make the use more comfortable, you could use the following startsWith function:
function startsWith($str, $prefix, $case_sensitivity=false) {
if ($case_sensitivity) {
return substr($str, 0, strlen($prefix)) === $prefix;
} else {
return strcasecmp(substr($str, 0, strlen($prefix)), $prefix) === 0;
}
}
Note that these functions do not support multi-byte characters as only bytes are compared. An equivalent function with multi-byte support could look like this:
function mb_startsWith($str, $prefix, $case_sensitivity=false) {
if ($case_sensitivity) {
return mb_substr($str, 0, mb_strlen($prefix)) === $prefix;
} else {
return mb_strtolower(mb_substr($str, 0, mb_strlen($prefix))) === mb_strtolower($prefix);
}
}
Here the character encoding of both strings is assumed to be the internal character encoding.
Use regular expressions
$var = "ab_123456789";
if(preg_match('/^ab_/', $var, $matches)){
/*your code here*/
}
You can use strpos():
$text = "ab_123456789";
if(strpos($text, "ab_") === 0)
{
// Passed the test
}
The easiest way would be to get a sub string. e.g. substr('ab_123456789', 0, 3);
It's quite easy, as your string can be accessed like an array of characters. So you just have to do something like :
if ($var[0] == "a" && $var[1] == "b" && $var[2] == "c")
return true
You also could use a find function from php library.