I am not good at expressions
I would like to match the string below of a string.
http://www.site.com/ * .js
preg_match('(http://www\.site\.com/).*(\.js)',$html,$match);
I know this code is not right. * Represents any file with .js extension.
Could anyone guide me with the expression.
Sorry if any duplication.
You have to use delimiters such as '#', '#' or '/' in the pattern :
$url = 'http://www.site.com/javascript/test.js';
$preg_match = preg_match('#(http://www\.site\.com/)(.*)(\.js)#', $url, $matches);
if($preg_match === 1)
{
var_dump($matches);
// displays :
// array
// 0 => string 'http://www.site.com/javascript/test.js' (length=38)
// 1 => string 'http://www.site.com/' (length=20)
// 2 => string 'javascript/test' (length=15)
// 3 => string '.js' (length=3)
}
else
{
// doesn't match
}
Related
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.
I need some help. What I want is to make ignore a comma in specific string. It is a comma seperated file csv, but the name have a comma, and I need to ignore that.
What I got is
<?php
$pattern = '/([\\W,\\s]+Inc.])|[,]/';
$subject = 'hypertext language, programming, Amazon, Inc., 100';
$limit = -1;
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$result = preg_split ($pattern, $subject, $limit, $flags);
?>
Result is
$result (php code):
<?php
array (
0 => 'hypertext language',
1 => ' programming',
2 => ' Amazon',
3 => ' Inc.',
4 => ' 100',
);
?>
And I want the result to be
$result (php code):
<?php
array (
0 => 'hypertext language',
1 => ' programming',
2 => ' Amazon, Inc.',
3 => ' 100',
);
?>
Thanks for your help :)
Note that [\W,\s] = \W since \W matches any char that is not a letter, digit or underscore. However, it seems you just want to split on a , that is not followed with space(s)*+Inc..
You may use a negative lookahead to achieve this:
/,(?!\s*Inc\.)/
^^^^^^^^^^^^
See the regex demo
The (?!\s*Inc\.) will fail any , match if there are 0+ whitespaces (\s*) followed with a sequence of literal characters Inc. after them.
From your tutorial, if I pull the Amazon information as a CSV, I get the following format. Which you can then parse with one of Php's native functions. This shows you don't need to use explode or regex to handle this data. Use the right tool for the job:
<?php
$csv =<<<CSV
"amzn","Amazon.com, Inc.",765.56,"11/2/2016","4:00pm","-19.85 - -2.53%",10985
CSV;
$array = str_getcsv($csv);
var_dump($array);
Output:
array (size=7)
0 => string 'amzn' (length=4)
1 => string 'Amazon.com, Inc.' (length=16)
2 => string '765.56' (length=6)
3 => string '11/2/2016' (length=9)
4 => string '4:00pm' (length=6)
5 => string '-19.85 - -2.53%' (length=15)
6 => string '10985' (length=5)
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;
I need to parse a lot of files and get their header declaration from all of them and add them all to an array..It doesnt matter if its the same or not since i'll use array_unique after to get only the unique once.
Some files have comments on the top so i can just pick the first line. The declaration is like this:
private ["_aaaaaaa", "_bbbbbb", "_ccccc", "_dddddddd"];
but sometimes it can be like this (no space)
private["_aaaaaaa","_bbbbbb","_ccccc","_dddddddd"];
or like this (if the guy who wrote it didnt pay attention)
private["_aaaaaaa", "_bbbbbb","_ccccc", "_dddddddd"];
So far i got this:
<?php
$str = 'private ["_aaaaaaa","_bbbbbb","_ccccc","_dddddddd"];';
$arr = Array();
$start = 'private [';
$end = '];';
$pattern = sprintf(
'/%s(.+?)%s/ims',
preg_quote($start, '/'), preg_quote($end, '/')
);
if (preg_match($pattern, $str, $matches)) {
list(, $match) = $matches;
echo $match;
}
?>
which outputs :
"_aaaaaaa","_bbbbbb","_ccccc","_dddddddd"
Still though that doesnt cover it....plus how will i make that to an array...?
Is there a simple way of doing this ? I've got the function that parses all the files in a folder and subfolder...i just need first to parse all the files and make this array which i'll later use in my main function.
Any help would be appreciated.
-Thanks
This should work -
/*
Function-> get_header()
Input -> The header string.
Output -> An array of header's parameters.
*/
function get_header($string){
if(preg_match("/private\s?\[(.*?)\];/", $string, $matches)){
return preg_split("/(\s*)?,(\s*)?/",$matches[1]);
}
return Array();
}
//Assuming these to be the different file headers.
$headers = Array(
'private ["_aaaaaaa", "_bbbbbb", "_ccccc","_dddddddd"];',
'private ["_4","_3","_2","_1" ];',
'private["_a", "_b","_c", "_d"];'
);
$header_arr = Array();
foreach($headers as $h){
$header_arr = array_merge($header_arr, get_header($h));
}
var_dump($header_arr);
OUTPUT-
/*
array
0 => string '"_aaaaaaa"' (length=10)
1 => string '"_bbbbbb"' (length=9)
2 => string '"_ccccc"' (length=8)
3 => string '"_dddddddd"' (length=11)
4 => string '"_4"' (length=4)
5 => string '"_3"' (length=4)
6 => string '"_2"' (length=4)
7 => string '"_1" ' (length=5)
8 => string '"_a"' (length=4)
9 => string '"_b"' (length=4)
10 => string '"_c"' (length=4)
11 => string '"_d"' (length=4)
*/
In PHP I have an array like this:
array
0 => string 'open' (length=4)
1 => string 'http://www.google.com' (length=21)
2 => string 'blank' (length=5)
but it could also be like:
array
0 => string 'blank' (length=5)
1 => string 'open' (length=4)
2 => string 'http://www.google.com' (length=21)
now it is easy to find "blank" with in_array("blank", $array) but how can I see if one string is starting with "http"?
I've tried with
array_search('http', $array); // not working
array_search('http://www.google.com', $array); // is working
now everything after `http? could vary (how to write vary, varie? could be different is what I mean!)
Now do I need a regex or how can I check if http exists in array string?
Thanks for advices
"Welcome to PHP, there's a function for that."
Try preg_grep
preg_grep("/^http\b/i",$array);
Regex explained:
/^http\b/i
^\ / ^ `- Case insensitive match
| \/ `--- Boundary character
| `------ Literal match of http
`--------- Start of string
Try using the preg_grep function which returns an array of entries that match the pattern.
$array = array("open", "http://www.google.com", "blank");
$search = preg_grep('/http/', $array);
print_r($search);
Solution without regex:
$input = array('open', 'http://www.google.com', 'blank');
$output = array_filter($input, function($item){
return strpos($item, 'http') === 0;
});
Output:
array (size=1)
1 => string 'http://www.google.com' (length=21)
You can use preg_grep
$match = preg_grep("/http/",$array);
if(!empty($match)) echo "http exist in the array of string.";
or you can use foreach and preg_match
foreach($array as $check) {
if (preg_match("/http/", $check))
echo "http exist in the array of string.";
}