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);
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
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
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 8 years ago.
Improve this question
When I type:
<?php
$temp="1234";
echo "<script type='text/javascript'>document.write({$temp});</script>";
?>
I get 1234 on the screen.
But when I replace $temp="1234" with $temp="alfa" I get nothing on the screen.
What's the problem ? Where am I wrong ? "1234" and "alfa" are both strings
alert(1234) is valid javascript, and will simply pop up the integer 1234 on your screen.
alert(alfa) is attempting to pop up the contents of a variable named alfa on your screen, which doesn't exist.
If you're insert data from PHP into a Javascript context, you MUST use json_encode() to ensure that you're producing VALID javascript. e.g.
$temp = 'alfa';
$json= json_encode($temp);
echo '<script>.... {$json}...</script>';
that will produce alert('alfa') and work as expected.
You aren't enclosing the text in the double-quote character '"' which denotes a string.
document.write(1234) works because 1234 is a valid value in JavaScript (an integer), whereas alfa is a symbol name, it should be "alfa".
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'm a newbie with PCRE in PHP. I'm trying to make a very basic shortcode function that could make something with a format like this one: {somealphanumericthing}
In essence I need a preg_match_all() that could find in my post these type of occurrences. I tried something like this:
$shortcode = preg_match_all('/^\b\{[a-zA-Z0-9_]+(\}\b)$/', $body, $found);
var_dump($shortcode);
if($shortcode==1) {
for($i=0;$i<count($found);$i++) {
print_r($found);
//do something nice
}
}
But unfortunately it's not working: I get int 0 to the test string {test}
A few things about the regular expression:
You don't need your line anchors since you are searching in a larger string.
There's not need to capture the closing }
Optimization, use the character class \w
Condensed:
/\b\{[a-zA-Z0-9_]+\}\b/
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);