Compilation failed : nothing to repeat at offset 19 (preg_match) - php

I have the following simple code which check if the password contains at least two lowercases.
preg_match("/^(?=.*[a-z].*[a-z])+$/")
But this gave me the following error message:
Compilation failed : nothing to repeat at offset 19.
I can't figure where I'm wrong
Later Edit
The following code which checks if i have at least two special characters works well:
preg_match("/^(?=.*[!##$%^&*].*[!##$%^&*])[a-zA-Z_!##%^&*]+$/")

Try this
<?php
preg_match("/^(.*[a-z].*[a-z].*)$/", "2313123g123123u123", $result);
var_dump($result);
preg_match("/^(.*[a-z].*[a-z].*)$/", "65665656s656565", $result);
var_dump($result);
?>
result
array(2) {
[0]=>
string(18) "2313123g123123u123"
[1]=>
string(18) "2313123g123123u123"
}
array(0) {
}

The (?= ) defines an assertion, you can not repeat an assertion. Did you mean to use (?: )?
$data = array('ab', '123a345b', '123');
foreach ($data as $subject) {
$found = preg_match("/^(?:.*[a-z].*[a-z])+$/", $subject, $match);
var_dump($found, $match);
}
Output:
int(1)
array(1) {
[0]=>
string(2) "ab"
}
int(1)
array(1) {
[0]=>
string(8) "123a345b"
}
int(0)
array(0) {
}

Related

$matches is returning same value in preg_match_all

I have array of links, i am trying to match using preg_match_all,regex but it is giving me the same result each and every time
foreach ($result[0] as $temp) {
preg_match_all($regex1, $temp["content"], $matches);
$storeUrl[]= $matches;
}
foreach ($storeUrl as $tem) {
preg_match_all($regex2, $tem[],$matches);
$storeUrllist [$count1++]= $matches;
}
$matches is working fine for first foreach and second foreach it is always returning same output only even it is not matching
I'm just guessing that you might want to design an expression that'd be similar to:
\b(?:https?:\/\/)?(?:www\.)?\w+\.(?:org|org2|com)\b
or:
\b(?:https?:\/\/)(?:www\.)?\w+\.(?:org|org2|com)\b
or:
\b(?:https?:\/\/)(?:www\.)\w+\.(?:org|org2|com)\b
not sure though.
Test
$re = '/\b(?:https?:\/\/)?(?:www\.)?\w+\.(?:org|org2|com)\b/m';
$str = 'some content http://alice.com or https://bob.org or https://www.foo.com or or baz.org2 ';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
Output
array(4) {
[0]=>
array(1) {
[0]=>
string(16) "http://alice.com"
}
[1]=>
array(1) {
[0]=>
string(15) "https://bob.org"
}
[2]=>
array(1) {
[0]=>
string(19) "https://www.foo.com"
}
[3]=>
array(1) {
[0]=>
string(8) "baz.org2"
}
}
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

preg_replace_callback, removing apostrophes

I have the following regular expression callback, and the issue that I am having, is that in the final result, the ' are removed. What can I do to prevent this from happening?
<?php
$eval = "'$server.REQUEST_URI' == '/user/photos'";
$result = preg_replace_callback('/\'(.+)\'/isU', function($matches){
$match = $matches[1];
$find = Object69::find($match);
return !empty($find) ? $find : $match;
}, $eval);
var_dump($result);
here is the result of $result
string(29) " /user/photos == /user/photos"
This is what I was expecting:
string(29) "'/user/photos' == '/user/photos'"
and here are the arrays that it finds $matches:
array(2) {
[0]=>
string(21) "'$server.REQUEST_URI'"
[1]=>
string(19) "$server.REQUEST_URI"
}
array(2) {
[0]=>
string(14) "'/user/photos'"
[1]=>
string(12) "/user/photos"
}

PHP Regular Expression to extract JSON data

I have the following string:
window['test'] = false;
window['options'] = true;
window['data'] = { "id" : 2345, "stuff": [{"id":704,"name":"test"};`
How would I go about extracting the JSON data in window['data']? The example data I provided is just a small sample of what really exists. There could be more data before and/or after window['data'].
I've tried this but had no luck:
preg_match( '#window["test"] = (.*?);\s*$#m', $html, $matches );
There are several issues that I can see.
Your string uses single quotes: window['test'] not window["test"], which you have in your regular expression. This means you should use double quotes to enclose your regular expression (or escape the quotes).
Your regular expression has unescaped brackets, which is used to create a character class. You should use \[ instead of just [.
You say you are looking for data but your regular expression looks for test.
You have a $ at the end of the regular expression, which means you won't match if there is nothing other than whitespace after the bit you matched.
Also your data seems incomplete, there are some missing brackets at the end, but I think that is just a copy-paste error.
So I would try:
php > preg_match("#window\['data'\]\s*=\s*(.*?);#", $html, $matches);
php > print_r($matches);
Array
(
[0] => window['data'] = {"id":2345,"stuff":[{"id":704,"name":"test"};
[1] => {"id":2345,"stuff":[{"id":704,"name":"test"}
)
Of course then you must use json_decode() to convert the JSON string ($matches[1]) into an object or associative array that you can use.
You can use this regex:
window\['data'\]\s*=\s*(.*?);
Working demo
The match information is:
MATCH 1
1. [67-111] `{"id":2345,"stuff":[{"id":704,"name":"test"}`
As regex101 suggests you could have a code like this:
$re = "/window\\['data'\\]\\s*=\\s*(.*);/";
$str = "window['test'] = false; window['options'] = true; window['data'] = {\"id\":2345,\"stuff\":[{\"id\":704,\"name\":\"test\"};";
preg_match_all($re, $str, $matches);
You can parse the window data with the regular expression:
/^window\[['"](\w+)['"]\]\s*=\s*(.+);\s*$/m
Then you can retrieve the pieces by their original index in the window data structures, and parse the JSON at your leisure.
$data = <<<_E_
window['test'] = false;
window['options'] = true;
window['data'] = { "id" : 2345, "stuff": [{"id":704,"name":"test"}]};
_E_;
$regex = <<<_E_
/^window\[['"](\w+)['"]\]\s*=\s*(.+);\s*$/m
_E_; // SO syntax highlighting doesnt like HEREDOCs "
if( preg_match_all($regex,$data,$matches) > 0 ) {
var_dump($matches);
$index = array_search('data',$matches[1]);
if( $index !== 0 ) {
var_dump(json_decode($matches[2][$index]));
} else { echo 'no data section'; }
} else { echo 'no matches'; }
Output:
// $matches
array(3) {
[0]=>
array(3) {
[0]=> string(24) "window['test'] = false; "
[1]=> string(26) "window['options'] = true; "
[2]=> string(69) "window['data'] = { "id" : 2345, "stuff": [{"id":704,"name":"test"}]};"
}
[1]=>
array(3) {
[0]=> string(4) "test"
[1]=> string(7) "options"
[2]=> string(4) "data"
}
[2]=>
array(3) {
[0]=> string(5) "false"
[1]=> string(4) "true"
[2]=> string(51) "{ "id" : 2345, "stuff": [{"id":704,"name":"test"}]}"
}
}
// decoded JSON
object(stdClass)#1 (2) {
["id"]=> int(2345)
["stuff"]=>
array(1) {
[0]=>
object(stdClass)#2 (2) {
["id"]=> int(704)
["name"]=> string(4) "test"
}
}
}
Note: I fixed the JSON in your example to be valid so it would actually parse.

Regex is returning only the first word from text

I have problem with my regex expression.
My regex return only first word from text. I want to return all word from this string.
Regex:
$test = "Reason: test test test";
$regex = "/Reason: (\w+)+/";
preg_match_all($regex, $test, $reason);
Returned code from var_dump($reason):
array(2) {
[0]=>
array(1) {
[0]=>
string(12) "Reason: test"
}
[1]=>
array(1) {
[0]=>
string(4) "test"
}
}
I want:
array(2) {
[0]=>
array(1) {
[0]=>
string(12) "Reason: test test test"
}
[1]=>
array(1) {
[0]=>
string(4) "test test test"
}
}
\w doesn't match whitespaces, only alphanumerical characters. That's why it stops when encountering the first .
If everything is text after the :, you may want to use
$regex = "/Reason: (.+)/"

check string in array for special characters

I have got an array wich contains several strings like this:
array(133) {
[0]=>
array(1) {
["row"]=>
array(5) {
[0]=>
string(10) "testd ' /% ata"
[1]=>
string(14) "testdata 111"
[2]=>
string(17) "testdata 123"
[3]=>
string(0) ""
[4]=>
string(0) ""
}
}
[1]=>
array(1) {
["row"]=>
array(5) {
[0]=>
string(9) "198"
[1]=>
string(14) "testdata"
[2]=>
string(41) "testdat"
[3]=>
string(0) ""
[4]=>
string(0) ""
}
}
My question is how can I check the strings within the array on special chars? These special chars are causing a syntax error when importing into my DB.
I think i need to use something like this?
preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);
Can anyone help me on this one?
Allright nog i have created this piece of code:
// search for special chars in the import data and remove them
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
foreach ($data_set as $data)
foreach ($data_set['data'] as $row) {
if(strpbrk($row, $illegal)) {
echo($row);
die();
}
else {
//not allowed ,escape or do what you want
echo("no characters found");
die();
}
var_dump($row);
die();
}
But this still gives an error:
A PHP Error was encountered
Severity: Warning
Message: strpbrk() expects parameter 1 to be string, array given
Filename: controllers/import.php
Line Number: 153
no characters found
You may have a look at strpbrk. It should solve your problem.
Example:
$tests = "testd ' /% ata";
);
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Contains special chars";
But i suggest to escape your strings before inserting to database,it's much safer.
foreach ($yourArray as $array)
foreach ($array['row'] as $row) {
if(strpbrk($row, $illegal)) {
//allowed ,insert to db
}
else {
//not allowed ,escape or do what you want
}
}

Categories