I currently have code that displays data like so:
1
11 Title Here
2
21 Guns
A
Awesome
Using this:
foreach($animes as $currentAnime){
$thisLetter = strtoupper($currentAnime->title[0]);
$sorted[$thisLetter][] = array('title' => $currentAnime->title, 'id' => $currentAnime->id);
unset($thisLetter);
}
How do I group all numbers to a #, and all Symbols to a ~?
Like so:
#
11 Title Here
21 Guns
~
.ahaha
A
Awesome
Thank you for the advice.
You can check with is_numeric() if this is a number and with preg_match() if this is a symbol.
foreach($animes as $currentAnime){
$thisLetter = strtoupper($currentAnime->title[0]);
if(is_numeric($thisLetter))
{
$sorted['#'][] = array('title' => $currentAnime->title, 'id' => $currentAnime->id);
}
else if(preg_match('/[^a-zA-Z0-9]+/', $thisLetter))
{
$sorted['~'][] = array('title' => $currentAnime->title, 'id' => $currentAnime->id);
}
else
{
$sorted[$thisLetter][] = array('title' => $currentAnime->title, 'id' => $currentAnime->id);
}
unset($thisLetter);
}
[^a-zA-Z0-9]+ - fits letter if there are no a-z letters also no A-Z characters nor 0-9 digits there can be added characters that will not suit as well you didn't precise what characters fits so I've added to regex basics. Moreover, you can check also if there is a letter only by [a-zA-Z]+ and add this to string group and last "else" statement will be for strings that aren't numeric neither strings.
Related
I want to filter strings that I have in an csv file, and I'm looking for a correct regexp that matches these strings:
PLP_LES_HALLES.VOLUME_POMPE
Newyork:Flow(m3/h)
In fact, the string should not contain any characters like : ç & é # ! ? “ ' ³ = + etc.
I tried this one :
([a-zA-Z0-9_:.(\/)]*) but when I tested it, I figured out that it matches everything. Kindly help me to find the correct one.
Here is my code to test:
while (($line = fgetcsv($handle, 1024, ";")) !== FALSE) {
$total = count( $line );
$keys = array('mesure', 'timestamp', 'value');
$args=array(
'mesure' => array('filter' => FILTER_VALIDATE_REGEXP,
'options' => array('regexp' => '([a-zA-Z0-9_:.(\/)]*)')),
'timestamp' => array( 'filter' => FILTER_VALIDATE_INT,
'options' => array('min_range' => 20000000000000, 'length' => 14)),
'value' => FILTER_VALIDATE_FLOAT);
$testing = filter_var_array(array_combine($keys, $line), $args);
var_dump($testing);
}
EDIT
These strings should not match:
PLP_LES_HALLéS.VOLUME_POMPE
PLP_LES_HàLLES.VOLUME_POMPE
Newyork:Flow(m³/h)
To sum up, all strings that have any characters from the list ç & é # ! ? “ ' ³ = + etc` should not match
Your regex does not match the whole string, and you are using ambiguous regex delimiter, it is recommended to use more common symbols as regex delimiters.
'/^[a-zA-Z0-9_:.()\/]*$/'
^^ ^^
The ^ will match the start of the string, and $ will match its end, requiring a whole string match.
Also, [a-zA-Z0-9_] can be written as \w, use it to shorten the pattern (this is not recommended only if you do not want to match Unicode strings):
'/^[\w:.()\/]*$/'
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)
$string = '/start info#example.com';
$pattern = '/{command} {name}#{domain}';
get array params in php, Like the example below:
['command' => 'start', 'name' => 'info', 'domain' => 'example.com']
and
$string = '/start info#example.com';
$pattern = '/{command} {email}';
['command' => 'start', 'email' => 'info#example.com']
and
$string = '/start info#example.com';
$pattern = '{command} {email}';
['command' => '/start', 'email' => 'info#example.com']
If its a single line string you can use preg_match and a regular expression such as this
preg_match('/^\/(?P<command>\w+)\s(?P<name>[^#]+)\#(?P<domain>.+?)$/', '/start info#example.com', $match );
But depending on variation in the data you may have to adjust the regx a bit. This outputs
command [1-6] start
name [7-11] info
domain [12-23] example.com
but it will also have the numeric index in the array.
https://regex101.com/r/jN8gP7/1
Just to break this down a bit, in English.
The leading ^ is start of line, then named capture ( \w (any a-z A-Z 0-9 _ ) ) then a space \s then named capture of ( anything but the #t sign [^#] ), then the #t sign #, then name captured of ( anything .+? to the end $ )
This will capture anything in this format,
(abc123_ ) space (anything but #)#(anything)
I am so bad at creating regex and I'm struggling with what I am SURE it's a simple stupid regex.
I am using PHP to do this match. Here is what I have until now.
Test string: 8848842356063003
if(!preg_match('/^[0-2]|[7-9]{16}/', $token)) {
return array('status' => 'failed', 'message' => "Invalid token", 'token' => '');
}
The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters. What am I doing wrong? Because I get, as a match:
array(
0 => 8
)
And I should get:
array(
0 => 8848842356063003
)
By the way: I am using PHP Live Regex to test my regex string.
Thanks in advance,
Ares D.
The regex must comply to this: Start with 0-2 or 7-9 and have EXACTLY 16 characters
You can put starting numbers in same character class and use end anchor after matching 15 more charaters:
/^[0-27-9].{15}$/
If you want to match only digits then use:
/^[0-27-9]\d{15}$/
So, I have a text field that can contain only letters, numbers, hyphens, dots and underscores. I would like to validate it using Zend_Validate_Regex but this pattern does not work. Why?
/[a-z][A-Z][0-9]-_./
Here is my text element:
$titleSlug = new Zend_Form_Element_Text('title_slug', array(
'label' => 'Title Slug',
'required' => FALSE,
'filters' => array(
'StringTrim',
'Null'
),
'validators' => array(
array('StringLength', FALSE, array(3, 255)),
array('Regex', FALSE, array('pattern' => '/[a-z][A-Z][0-9]-_./'))
)
));
Your regex matches a string that contains a lowercase letter, an uppercase letter, a digit, a dash, an underscore and any other character, in that order. You need this:
/^[\w.-]*$/
^ and $ anchor the match at the start and end of the string.
\w matches letters, digits and underscore; together with the dot and dash they form a character class ([...]) which is repeated zero or more times (*).
how about this:
/[a-zA-Z]*|\d*|-*|\.*|_*/