how to fix preg_match() unknown modifier '?' in localeconv() - php

the problem s in the 'decimal_point' => string '/'
but did anyone have a good solution to fix it?
the error:
preg_match() unknown modifier '?'
the function :
* #param string $str input string
* #return boolean
*/
public static function numeric($str)
{
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
// A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
return (bool) preg_match('/^-?+(?=.*[0-9])[09]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
}
it's localeconv() dump :
array (size=18)
'decimal_point' => string '/' (length=1)
'thousands_sep' => string ',' (length=1)
'int_curr_symbol' => string 'IRR' (length=3)
'currency_symbol' => string 'ريال' (length=8)
'mon_decimal_point' => string '/' (length=1)
'mon_thousands_sep' => string ',' (length=1)
'positive_sign' => string '' (length=0)
'negative_sign' => string '-' (length=1)
'int_frac_digits' => int 2
'frac_digits' => int 2
'p_cs_precedes' => int 0
'p_sep_by_space' => int 0
'n_cs_precedes' => int 0
'n_sep_by_space' => int 0
'p_sign_posn' => int 3
'n_sign_posn' => int 3
'grouping' =>
array (size=1)
0 => int 3
'mon_grouping' =>
array (size=1)
0 => int 3
the relate issue on github
koseven/issues #351

Since you're using / as the delimiter in your regexp, pass it to the preg_quote function as well, as the second parameter:
return (bool) preg_match('/^-?+(?=.*[0-9])[09]*+' . preg_quote($decimal, '/') . '?+[0-9]*+$/D', (string) $str);
// -----------------------------------------------------------------------^
Quote from the manual:
Note that / is not a special regular expression character.
[...]
delimiter
If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required
by the PCRE functions. The / is the most commonly used delimiter.

Related

Removing whitespace string from a string variable in php

I tried removing the whitespace but don´t work. I use trim for remove spaces.
I realized in php, for callback in a responseText ajax.
$orderHTML = $producto['id'].'#'.$producto['nombre_producto'].'*'.$producto['precioVenta'].'*'.$producto['descripcion'].'*'.$producto['descatalogado'].'#'.$producto['cantidad_stock'];
echo trim($orderHTML);
In my ajax the result data is:
data: " 1#jeans*1.00**0#100"
I´ve got my call a php is:
GET "http://localhost:8080/ajax/products_ajax.php?idProducto=1&opcion=2"
My php is:
<?php
require_once '../../vendor/autoload.php';
require_once '../../config.php';
require_once '/functions/function_orders.php';
$opcion = $_REQUEST['opcion'];
switch($opcion)
{
case '1':
if(isset($_POST['parametro1'])&&isset($_POST['parametro2']))
{
$orderHTML = getOrdersProduct($_POST['parametro1'],$_POST['parametro2']);
echo trim($orderHTML);
}
break;
case '2':
if(isset($_GET['idProducto']))
{
$producto = getOrdersProduct1($_GET['idProducto']);
$orderHTML = trim($producto['id']).'#'.$producto['nombre_producto'].'*'.$producto['precioVenta'].'*'.$producto['descripcion'].'*'.$producto['descatalogado'].'#'.$producto['cantidad_stock'];
echo trim($orderHTML);
}
}
My query in idiorm:
function getOrdersProduct1($identificador)
{
return ORM::for_table('producto')->
where('id',$identificador)->find_one()->as_array();
}
I realize one var_dump($productos);die();
array (size=11)
'id' => string '1' (length=1)
'nombre_producto' => string 'jeans' (length=6)
'nombre_latin' => null
'peso' => string '100.00' (length=6)
'descatalogado' => string '0' (length=1)
'dimensiones' => null
'descripcion' => null
'cantidad_stock' => string '100' (length=3)
'precioVenta' => string '1.00' (length=4)
'gama_id' => string '2' (length=1)
'proveedor_id' => string '1' (length=1)
What am I doing wrong? thanks
you have to use the trim function with the second argument correctly. if all that fails try
$str = trim(preg_replace('/\s+/',' ', $str));
the line of code will remove extra spaces, as well as leading and trailing spaces. this is combined with trim and preg_replace.
It looks to me that your $producto['id'] is a text field of fixed length and therefore has leading spaces.
You should use trim() or ltrim() to remove these before concatenating the values into the $orderHTML, then later when you come to read the code it will be self documenting as to why you had to do this manipulation, like so
$orderHTML = ltrim($producto['id']) . '#' .
$producto['nombre_producto'].'*'.
$producto['precioVenta'].'*'.
$producto['descripcion'].'*'.
$producto['descatalogado'].'#'.
$producto['cantidad_stock'];
echo $orderHTML;

Import information from file to mysql with php

How can i import information from a file (like below) to my mysql database by a php file?
I was thinking by open a file, insert a row into a variabele and check every line for an = mark and insert the information into array's by checking the tag bevore the =.
Is this the right way or are there better ways.
Example file:
# Disc length: 926 seconds
# Revision: 0
# Processed by: cddbd v1.5PL3 Copyright (c) Steve Scherf et al.
# Submitted via: audiograbber 1.83.01
DISCID=00039c14
DTITLE=3M Littmann Stethoscope Edition / 20 Beispiele zur Herz- und Lungenausku
DTITLE=ltation
DYEAR=1997
DGENRE=Medizin
TTITLE0=Normale Herztöne
TTITLE1=Dritter Herzton (physiologisch)
TTITLE2=Vierter Herzton
TTITLE3=Aortenklappenstenose
TTITLE4=Mitralklappeninsuffizienz
TTITLE5=Mittelsystolischer Klick
TTITLE6=Ventrikelseptum-Defekt
TTITLE7=Atriumseptum-Defekt
TTITLE8=Mitralklappenstenose
TTITLE9=Aortenklappeninsuffizienz
TTITLE10=normales tracheales Atemgeräusch
TTITLE11=normales vesikuläres Atemgeräusch
TTITLE12=feine Krepitation mit leichter bronchialer Atmung
TTITLE13=rauhe Krepitation
TTITLE14=bronchiale Atmung
TTITLE15=Stridor beim Einatmen
TTITLE16=Rhonchus
TTITLE17=pfeifender Rhonchus (Keuchatmen)
TTITLE18=feine Krepitation (Knisterrasseln)
TTITLE19=pleurales Reibungsgeräusch
EXTD=
EXTT0=
EXTT1=
EXTT2=
EXTT3=
EXTT4=
EXTT5=
EXTT6=
EXTT7=
EXTT8=
EXTT9=
EXTT10=
EXTT11=
EXTT12=
EXTT13=
EXTT14=
EXTT15=
EXTT16=
EXTT17=
EXTT18=
EXTT19=
PLAYORDER=
Try this:
$settingArray = array();
$text = file('file.txt');
foreach($text as $line){
$line = str_replace(array("\r", "\n"), '', $line);
if(substr($line, 0, 1) != '#' && !empty($line)){
$key = substr($line, 0, strpos($line, "="));
$value = substr($line, strpos($line, "=") + 1);
$value = substr($value, 0, strpos($value, "#") != 0 ? strpos($value, "#") : strlen($value)); //removes everything behind a # sign
$settingArray[$key] = $value;
}
}
var_dump($settingArray);
/*this will print:
array (size=46)
'DISCID' => string '00039c14' (length=8)
'DTITLE' => string 'ltation' (length=7)
'DYEAR' => string '1997' (length=4)
'DGENRE' => string 'Medizin' (length=7)
'TTITLE0' => string 'Normale Herztöne ' (length=17)
'TTITLE1' => string 'Dritter Herzton (physiologisch)' (length=31)
'TTITLE2' => string 'Vierter Herzton' (length=15)
'TTITLE3' => string 'Aortenklappenstenose' (length=20)
'TTITLE4' => string 'Mitralklappeninsuffizienz' (length=25)
'TTITLE5' => string 'Mittelsystolischer Klick' (length=24)
'TTITLE6' => string 'Ventrikelseptum-Defekt' (length=22)
'TTITLE7' => string 'Atriumseptum-Defekt' (length=19)
'TTITLE8' => string 'Mitralklappenstenose' (length=20)
'TTITLE9' => string 'Aortenklappeninsuffizienz' (length=25)
'TTITLE10' => string 'normales tracheales Atemgeräusch' (length=32)
'TTITLE11' => string 'normales vesikuläres Atemgeräusch' (length=33)
'TTITLE12' => string 'feine Krepitation mit leichter bronchialer Atmung' (length=49)
'TTITLE13' => string 'rauhe Krepitation' (length=17)
'TTITLE14' => string 'bronchiale Atmung' (length=17)
'TTITLE15' => string 'Stridor beim Einatmen' (length=21)
'TTITLE16' => string 'Rhonchus' (length=8)
'TTITLE17' => string 'pfeifender Rhonchus (Keuchatmen)' (length=32)
'TTITLE18' => string 'feine Krepitation (Knisterrasseln)' (length=34)
'TTITLE19' => string 'pleurales Reibungsgeräusch' (length=26)
'EXTD' => boolean false
'EXTT0' => boolean false
'EXTT1' => boolean false
'EXTT2' => boolean false
'EXTT3' => boolean false
'EXTT4' => boolean false
'EXTT5' => boolean false
'EXTT6' => boolean false
'EXTT7' => boolean false
'EXTT8' => boolean false
'EXTT9' => boolean false
'EXTT10' => boolean false
'EXTT11' => boolean false
'EXTT12' => boolean false
'EXTT13' => boolean false
'EXTT14' => boolean false
'EXTT15' => boolean false
'EXTT16' => boolean false
'EXTT17' => boolean false
'EXTT18' => boolean false
'EXTT19' => boolean false
'PLAYORDER' => boolean false*/
You should simple ignore lines starting with # and then in all other lines (not-empty) find first occurrence of = sign and using susbtr (or mb_substr) you will get columns and columns values.
EDIT
Someone suggest it's INI file however in fact it isn't so you cannot use parse_ini_file function. First thing is that comments here start with '#` what would generate Deprecated warning (ini PHP 5.5.12) and in addition because no quotes were used you will get warning :
Warning: syntax error, unexpected '(' in test.ini on line 12
and this function will return false
EDIT2
I would do it this way:
<?php
$data = array();
$lines = file('test.ini');
mb_internal_encoding('UTF-8');
foreach($lines as $line){
$line = trim($line);
if ($line == '' || mb_substr($line,0,1) == '#') {
continue;
}
$pos = mb_strpos($line, '=');
if ($pos == false) { // == not ===
continue;
}
$data[mb_substr($line,0,$pos)] = mb_substr($line, $pos + 1);
}
var_dump($data);
You need to trim each line first and remove empty lines and lines starting with # as I said before, then you check for occurrence of =. If it's not found or at 0 position simple you need to ignore this line, all other lines are placed into $data array

Retrieving view data in a view composer

If I do the following in this view composer:
View::composer('product.edit', function($view)
{
$viewdata = $view->getData();
dd($viewdata);
});
I can see in the output that exists 'language_id', however I don't know how to retrieve it, since $viewdata['language_id'] will throw an exception.
How should I go about this?
Update:
The accepted answer led me to discover that when a presenter is involved in this operation, the model will be available in the offset, here is the final code:
View::composer('product.edit', function($view)
{
$data = $view->offsetGet('product')->toArray();
echo $data['language_id'];
exit;
});
You may try this:
$language_id = $view->offsetGet('language_id');
Following public methods are available in the $view object (An instance of Illuminate\View\View)
0 => string '__construct' (length=11)
1 => string 'render' (length=6)
2 => string 'renderSections' (length=14)
3 => string 'with' (length=4)
4 => string 'nest' (length=4)
5 => string 'withErrors' (length=10)
6 => string 'getEnvironment' (length=14)
7 => string 'getEngine' (length=9)
8 => string 'getName' (length=7)
9 => string 'getData' (length=7)
10 => string 'getPath' (length=7)
11 => string 'setPath' (length=7)
12 => string 'offsetExists' (length=12)
13 => string 'offsetGet' (length=9)
14 => string 'offsetSet' (length=9)
15 => string 'offsetUnset' (length=11)
16 => string '__get' (length=5)
17 => string '__set' (length=5)
18 => string '__isset' (length=7)
19 => string '__unset' (length=7)
20 => string '__call' (length=6)
21 => string '__toString' (length=10)
You may also try something like this:
if($view->offsetExists('language_id')) {
$language_id = $view->offsetGet('language_id');
}
Worth noting you can also access view data using the attribute syntax (at least starting Laravel 8):
$languageId = $view->product->language_id ?? null;

Using preg_replace on an an array rather than a string

I'm using the PHP preg_replace function on a string called $where:
$where = preg_replace( '/data\./', '', $where );
However I'd like to achieve the same expression replacement if $where is an array of an unknown size.
Do I have to set up a loop? Or is there a PHP function to help me out?
Any help would be appreciated.
Many thanks
Okay, here is my input array:
array
0 => null
1 => null
2 =>
array
'condition' =>
array
'column' => string 'start' (length=5)
'operator' => string '>=' (length=2)
'argvalue' => string '2013-11-21 00:00:00' (length=19)
3 =>
array
'condition' =>
array
'column' => string 'start' (length=5)
'operator' => string '<=' (length=2)
'argvalue' => string '2013-11-21 23:59:59' (length=19)
4 => null
Here is my manipulation:
$where = preg_replace('/start\./', 'Alan', $where );
And here is my output:
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string 'Array' (length=5)
3 => string 'Array' (length=5)
4 => string '' (length=0)
This is returning an Array to String conversion error.
Thanks
PHP docs on the subject argument of preg_replace
subject
The string or an array with strings to search and replace.
If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well.
Try this code :
$text = array( "editpostiddata" ,"editpodatastcat", "ch114","ch112");
$result = preg_replace(array_fill(0, 4, "/data/i"),'', $text);
print_r($result);

multiarray - how to keep first 3 index groups of each source type

I have a multi-array where I need to keep the first 3 index groups and remove the rest from the multiarray (in each group).
See multiarray here: https://gist.github.com/no1uknow/6887497
So:
In this example I need the multi-array to keep: The first 3 Heavy, Lite, Intermediate, etc (these are identified by the source_type_cd)
Example of the Lite part of the array after the first 3 are kept:
0 =>
array (size=9)
'validated_ata' => string '25' (length=2)
'source_type_cd' => string 'Lite' (length=4)
'validated_subata' => string '22' (length=2)
'action_cd' => string '3' (length=1)
'object_cd' => string '5' (length=1)
'malfunction_cd' => string '29' (length=2)
'corrective_action_txt' => string 'Repair-Passenger Seat-Loose / Displaced' (length=39)
'rec_count' => string '00050' (length=5)
'group_id' => int 48
1 =>
array (size=9)
'validated_ata' => string '25' (length=2)
'source_type_cd' => string 'Lite' (length=4)
'validated_subata' => string '22' (length=2)
'action_cd' => string '3' (length=1)
'object_cd' => string '5' (length=1)
'malfunction_cd' => string '1' (length=1)
'corrective_action_txt' => string 'Repair-Passenger Seat-Inoperative' (length=33)
'rec_count' => string '00047' (length=5)
'group_id' => int 44
2 =>
array (size=9)
'validated_ata' => string '25' (length=2)
'source_type_cd' => string 'Lite' (length=4)
'validated_subata' => string '22' (length=2)
'action_cd' => string '3' (length=1)
'object_cd' => string '5' (length=1)
'malfunction_cd' => string '31' (length=2)
'corrective_action_txt' => string 'Repair-Passenger Seat-Worn / Chaffed / Frayed' (length=45)
'rec_count' => string '00042' (length=5)
'group_id' => int 50
You will simply have to loop through the array and look if it has any of those values and place into a new array.
Example (where $arr is your multiarray):
// My silly solution for knowing what to look for
// When one is found, it will be removed from the array.
$find = array('Lite','Lite','Lite','Intermediate','Intermediate','Intermediate','Heavy','Heavy','Heavy');
// New array where your the values you want will be placed in
$new_arr = array();
foreach($arr as $v) {
// No need to keep looking if there's no more to find.
if(empty($find))
break;
// Look in $find array if current "source_type_cd" is still sought-after
$key = array_search($v['source_type_cd'], $find);
if($key !== false) {
$new_arr[] = $v; // Add to new array
unset($find[$key]); // Remove from "find" array
}
}
Thanks Colandus I actually figured it out like this... I was trying to avoid to many loops.
In a loop above this I set the source_type_cd in an array:
$groups[]=$value['source_type_cd']
Next I loop through the array and take the top 3 of each group by using array_splice twice and then merging back into a new array.
(also, I'm using a start and end point ($group_count).)
$start = 0;
$group_count = $i+1;
$top_count = 3;
foreach($groups as $k => $v) {
$top_count_array = array_merge((array)$top_count_array, (array)array_slice(array_slice($sorted_array, $start, $group_count, true),0,$top_count,true));
$start = $start+$group_count;
}
var_dump($top_count_array);
Again appreciate the input. Tried to shorten this code down from so many loops. Also requirements will change for grabbing the amount of $top_count and $group_count... Needed something a little more dynamic. :-)

Categories