Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to validade a string in the following format:
numbers, letters and _
Minimum length 4 and max length 15
At least 1 letter [a-z]
For example:
Valid:
ABCD
ABCDE
ABC_
01A_
A12345_BCDW1234
Not Valid:
ABC
01A
A12345_BCDW123411
_1212392034
_
A_1
I made a couple tries but none work.
I don't think you can do this in just one regex, the validation of constraints on length {4,15} and "must contains a letter" must be done independently.
$test_inputs = array(
'ABCD', 'ABCDE', 'ABC_', '01A_', 'A12345_BCDW1234'
, 'ABC', '01A', 'A12345_BCDW123411', '_1212392034', '_', 'A_1'
);
$res = array();
foreach($test_inputs as $input)
{
$res[$input] = (preg_match('/^[A-Z0-9_]{4,15}$/i', $input) && preg_match('/[A-Z]/i', $input));
}
var_dump($res);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help me get the value of IFC/USD 0.00001031 from the webpage http://infinitecoin.com.
I need to grab the value after IFC/USD..
The way I thought to do it was to:
$file_string = file_get_contents('http://infinitecoin.com');
preg_match('/IFC/USD plus next 11 charecters', $file_string, $title);
$value = $title[1];
echo $title_out ;
But im not sure how to ask PHP to find IFC/USD then return the next 11 characters after that.
If I could accomplish this, my task would be solved..
Any help would be great.
Thank you
Jason
$output = array();
preg_match("/(?<=IFC\/USD\s)[\d\.]+/", 'IFC/USD 0.00001015', $output);
$output will look like this:
Array
(
[0] => 0.00001015
)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to see if the last two characters/digits in a filename are numbers in PHP.
if (CODE HERE) {
// runs script because last two characters are numbers
}
This should set it off:
http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs20.jpg
The last two digits are '20'
This should not:
http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg
There are no last two digits
This should do the trick.
<?php
$filename = "http://www.nws.noaa.gov/weather/images/fcicons/skc23.jpg";
$posOfPeriod = strrpos($filename, ".");
$last2digits = substr($filename, $posOfPeriod -2, 2);
if (is_numeric($last2digits)) {
echo "Numeric: ".$last2digits;
}
else {
echo "Non-Numeric: ".$last2digits;
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using php (preg_match) to validate if a variable is float.
Been searching online but still have not found the one that i need. Pls advise.
my requirements:
VALID
(numbers and one dot only)
0.1 (anything starting with 0, must follow by a dot and then number)
1.1234567890 (max. 10 decimal places)
NOT VALID
blank/spaces
0
0.
0.0
00.0
0.0.0
01
integers
+0.1 (no plus signs)
-0.1 (no minus signs)
.1
01.1
1.
0.1e38 (no exponential)
Solutions:
/^(?=.*[1-9])(?!0\d)([0-9]{1,10})(\.[0-9]{1,10})$/
Quite simply this:
if (($num = filter_var($var, FILTER_VALIDATE_FLOAT)) !== false) {
echo "Yay $num is a float!\n";
}
You can use:
preg_match_all('!\d+(?:\.\d+)?!', $str, $matches);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have a file in a directory named 'achieve3000_Gestalt_students102713.txt'.. i want to reduce the file name to Gestalt_students ONLY
Have tried using explode() which gives something different altogether and preg replace
Please help
EDIT
i have 4 files
achieve3000_Gestalt_students102713.txt
achieve3000_Gestalt_teachers102713.txt
achieve3000_Perspectives_Middle_Academy_students102713.txt
achieve3000_Perspectives_Middle_Academy_teachers100913.txt
If the file name always begins with a character sequence whose length is equal to the length of "achieve3000_" (which is 12) and the number at the end is always 6 digits plus a 4 character extension sequence. The you can use this code
$output = substr($input, 12, strlen($input) - 22);
var_dump(substr('achieve3000_Gestalt_students102713.txt', 12, 16));
Did you try substr?
Like :
$input = "achieve3000_Gestalt_students102713.txt";
$answer =substr ($input, 12, 16);
echo $answer;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove a particular match of characters from a string.
For example i have strings;
topics-p10-new-model-cars
topics-p20-new-model-cars
topics-p30-new-model-cars
topics-p40-new-model-cars
Then i need the results as,
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
That means i want to remove p10-,p20-,etc..
.Those are the page numbers. It may be any number..
How can i do this..? Thanks in advance
Try this:
$result = preg_replace('/\-p\d+/', '', $string);
Note: I'm assuming that the string format does not change (I mean this [topics-p10-new-model-cars]). If my assumption is right.
Then you can do this
if (textBox1.Text.Contains("-p10-"))
{
//topics-p10-new-model-cars
String[] splited = textBox1.Text.Split(new char[] {'-'});
String rString = String.Format("{0}-{1}-{2}-{3}",
splited[0],splited[2],splited[3],splited[4]);
MessageBox.Show(rString);
}
//OR This method
if (textBox1.Text.Contains("-p10-"))
{
String result = textBox1.Text.Replace("p10-", "");
MessageBox.Show(result);
}
With 'preg-replace'::
For, example:
<?
echo preg_replace('/p[0-9]+\-/', '', 'topics-p10-new-model-cars');
?>
Follow this link:
http://www.php.net/manual/en/function.preg-replace.php