notepad++ regular expression result deleted () - regex

I'm trying to add a prefix to the string:
array(
'id'=>'page-comment',
'type' => 'switch',
'title' => 'Show Comment Form on Page',
'default' => '0',
'on' => 'Yes',
'off' => 'No',
),
Replace: 'title' => '(.*)'
to: 'title' => __('\1\9', 'venedor')
result
'title' => __'Show Comment Form on Page', 'venedor',
I need to characters () remained the same.
Line should look like result
'title' => __('Show Comment Form on Page', 'venedor'),

You're close, you have to escape the parenthesis even in the replacement part:
'title' => __\('$1', 'venedor'\)
It' a Npp "feature"

Related

Regex Input Filter in zend framework 2

i add input filter by this parameters :
and the question is why zend framework 2 have problem with utf8 pattern
$formInputFilter->add($inputFactory->createInput(array(
'name' => $field->attribute->id,
'required' => true,
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[0-9,\x{0600}-\x{06FF}]*/u',
'messages' => array(
'regexNotMatch'=>'number is not correct'
),
),
),
),
)
)
);
the error is :
preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 15
I use this in my urls. It works with any digits and characters
'\/^\d{4},(?:\s|\w)+\/u*'
https://regex101.com/r/mW4xJ4/2
\/^\d(?:\s|\w)+\/u*
You can see the explanations of each character here: https://regex101.com/r/mW4xJ4/1

ZF2 form validation not working properly for email

i have a input box with type="email" and validation it with zend validator
<input type="email" name="email" > email </input>
'email' => array(
'required' => true,
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
\Zend\Validator\Regex::NOT_MATCH=>'Please fill correct email ',
)
),
'break_chain_on_failure' => true
),
),
problem is if i use another array to check for
IS_EMPTY
zend regex again does not display the above error instead display default zend regex error and if i remove IS_EMPTY then it works fine. regex error is
1. input does not match to expression '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/'
EXACTLY NOT ABLE TO UNDERSTAND WHY THIS WORKS FINE WITHOUT EMPTY CHECK AND DOESN'T WORK ALONG WITH IT
You don't need to use notEmpty, you only need to set the field as "required" and specify the error message:
$this->add(array(
'name' => 'email',
'required' => true,
'error_message' => 'Please entry e-mail.',
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array (
'messages' => array(EmailAddress::INVALID => 'Please specify a valid e-mail.'),
),
'break_chain_on_failure' => true,
),
),
));
I have also the same dilemma where this "input does not match to expression" always appears instead of the message for EmailAddress::INVALID_FORMAT. But I found that the code you posted fixed the same error I had. This is my code.
'validators' => array(
array (
'name' => 'Regex',
'options' => array(
'pattern'=>'/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
'messages' => array(
Regex::NOT_MATCH => 'Please provide a valid email address.',
),
),
'break_chain_on_failure' => true
),
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
EmailAddress::INVALID_FORMAT => 'Please provide a valid email address.',
EmailAddress::DOT_ATOM => '',
EmailAddress::INVALID_FORMAT => '',
EmailAddress::INVALID_LOCAL_PART => '',
EmailAddress::QUOTED_STRING => '',
)
),
),
),

Cakephp validating if my user checked at least one option

I have a problem validating if my user checked at least one option from a list of checkboxes.
Here is what i tried:
My view looks like this:
echo $this->Form->input('market_segment_targeted', array(
'multiple' => 'checkbox',
'label'=>array('text' => 'Market segment targeted', 'class'=>'w120'),
'options' => array(
'Home users' => 'Home users',
'SOHO' => 'SOHO',
'SMB' => 'SMB',
'Enterprise' => 'Enterprise'
),
));
In my controller i have added this snippet of code:
$validate_on_fly = array(
'market_segment_targeted' => array(
'notEmpty' => array(
'rule' => array('multiple', array('min' => 1)),
'required' => true,
'message' => 'Please select at least one!'
))
)));
$this->Partner->validate = Set::merge(
$this->Partner->validate,
$validate_on_fly
);
Any ideas what am i doing wrong?
Thank you
In CakePHP you can use Model Validation for checkboxes. Here is a quick example.
Your Form can look like:
$this->Form->create('User');
$this->Form->input('User.agree', array('type'=>'checkbox', 'hiddenField'=>false, 'value'=>'0'));
$this->Form->submit('Save'):
$this->Form->end();
Then in your Model under public $validate, use:
'agree'=>array(
'Not empty'=>array(
'rule'=>array('comparison', '!=', 0),
'required'=>true,
'message'=>'You must agree to the ToS'
)
)

How to exclude characters from a RegEx pattern with category property codes?

There is a number of category property codes (see part "Unicode character properties"), that can be used for a Perl-compatible Regular Expression (PCRE)
I defined a regex pattern (named subpattern), that should match letters (\p{L}), numbers (\p{N}), the space separator (\p{Zs}), but also the punctuation (\p{P}).
(?<sport>[\p{L}\p{N}\p{Zs}\p{P}]*)
Since I'm using that for URL routing, the slashes should be excluded. How can I do that?
EDIT:
Addtitional information about the context: The pattern is used for a route definition in a Zend Framework 2 module.
/Catalog/config/module.config.php
<?php
return array(
...
'router' => array(
'routes' => array(
...
'sport' => array(
'type' => 'MyNamespace\Mvc\Router\Http\UnicodeRegex',
'options' => array(
'regex' => '/catalog/(?<city>[\p{L}\p{Zs}]*)/(?<sport>[\p{L}\p{N}\p{Zs}\p{P}]*)',
'defaults' => array(
'controller' => 'Catalog\Controller\Catalog',
'action' => 'list-courses',
),
'spec' => '/catalog/%city%/%sport%',
),
'may_terminate' => true,
'child_routes' => array(
'courses' => array(
'type' => 'segment',
'options' => array(
'route' => '[/page/:page]',
'defaults' => array(
'controller' => 'Catalog\Controller\Catalog',
'action' => 'list-courses',
),
),
'may_terminate' => true,
),
)
),
),
),
...
);
You can use negative look-ahead to exclude some character from your character set. For your example:
(?<sport>(?:(?!/)[\p{L}\p{N}\p{Zs}\p{P}])*)
Basically, you will check that the next character is not / with negative look-ahead (?!/), before proceeding to check whether that character belongs to the character set [\p{L}\p{N}\p{Zs}\p{P}].
PCRE doesn't have set intersection or set difference feature, so this is the work-around for that.
Since you use it for URL parsing:
According to RFC 1738 only $-_.+!*'(), are allowed unencoded in an URL¹, so instead of using \pP (yes that is allowed instead of \p{P}), I suggest you use these characters directly in your regex.
Edit: But if that's not an option, this should be a starting point
(?:([\p{L}\p{N}\p{Zs}\p{P}]+?)(?=/|\?|#|$))
kind regards,
Tom
¹: Not entirely true, but /##;?&= are only allowed unencoded if they should have their special meaning.

CakePHP routes regular expression for unicode

I want to do the following change in CakePHP routes. Any word or group of words (separated by hyphen) after a website URL like example.com/someword or example.com/some-word should be redirected to controller => posts, action => view. But my problem is that the word(s) should be in unicode. I have tried this regular expression for URL text, but it does not work.
Router::connect ( '/:link', array ('controller' => 'posts', 'action' => 'view' ), array ('pass' => array ('link'), 'link' => '/^['.json_decode('"\u0531"').'-'.json_decode('"\u0587"').'-]+$/u' ) );
I want to mention though, that my regex
'/^['.json_decode('"\u0531"').'-'.json_decode('"\u0587"').'-]+$/u',
works just fine if I am about to do some usual preg_match like
$regg = '/^['.json_decode('"\u0531"').'-'.json_decode('"\u0587"').'-]+$/u';
if (preg_match($regg, "my unicode string", $match)) {
var_dump($match); // outputs string as expected
}
die;
EDIT
I end up having this code
Router::connect ( '/:link', array ('controller' => 'posts', 'action' => 'view' ), array ('pass' => array ('link'), 'link' => '((?![a-zA-Z]).)+' ) );
like making my url to contain everything, but English letters, but this is not the thing I need, if I have 2 or more different languages than English - this method won't work. So, I want to send each language's word into separate action.
cake version 2.x
EDIT 2
There are alternative solutions, like in answers below, to bypass the question, but I am looking for the exact way of doing what I need.
Thanks
From http://en.wikipedia.org/wiki/Query_string at URL encoding paragraph you can't use these chars on a url query string:
In particular, encoding the query string uses the following rules:
Letters (A–Z and a–z), numbers (0–9) and the characters '.','-','~' and '_' are left as-is
SPACE is encoded as '+' or "%20" [8]
All other characters are encoded as %HH hex representation with any non-ASCII characters first encoded as UTF-8 (or other specified
encoding)
If you have two (or more) languages you can consider add on posts table one slug field for each language, for example with english and russian:
posts
id
title_eng
title_rus
slug_eng
slug_rus
On Post.php beforeValidate or beforeSave:
if(isset($this->data['Post']['title_eng'])){
$this->data['Post']['slug_eng'] = Inflector::slug(strtolower($this->data['Post']['title_eng']), '-');
}
if(isset($this->data['Post']['title_rus'])){
$this->data['Post']['slug_rus'] = Inflector::slug(strtolower($this->data['Post']['title_rus']), '-');
}
The link inside a view:
echo $this->Html->link($post['Post']['title_eng'], array('controller' => 'posts', 'action' => 'view', 'slug_eng' => $post['Post']['slug_eng']));
echo $this->Html->link($post['Post']['title_rus'], array('controller' => 'posts', 'action' => 'view', 'slug_rus' => $post['Post']['slug_rus']));
The route:
Router::connect('/posts/:slug_eng', array('controller' => 'posts','action' => 'view', 'eng'), array('pass' => array('slug_eng'), 'slug_eng' => '[a-zA-Z0-9]+'));
Router::connect('/ru/posts/:slug_rus', array('controller' => 'posts','action' => 'view', 'rus'), array('pass' => array('slug_rus'), 'slug_rus' => '[a-zA-Z0-9]+'));
The controller Posts:
public function view($locale, $slug){
....
validate and sanitize here
...
$post = $this->Post->find('first', array('conditions' => array('slug_' . $locale => $slug)));
....
}
I suggest to use wildcard
Router::connect('/*', array('controller' => 'posts', 'action' => 'view'));
Any other routes can be added before this line, for example
Router::connect('/:digits', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('digits'), 'digits' => '/d+');
Router::connect('/*', array('controller' => 'posts', 'action' => 'view'));