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;
}
?>
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 9 years ago.
Improve this question
I have a string : http://www.mywebsite/456754567/531613490.htm?menu=contact
I want to get the value between "/" and ".htm". Here : 531613490
Any idea ?
if ( preg_match('~[^/]+(?=\.htm)~', $string, $matches) ) {
echo $matches[0];
}
Here's a demo: http://codepad.viper-7.com/5kjEj6
I don't know how flexible you want your script to be, but here's my try:
It always takes the last part of the path
It won't fail for http://www.mywebsite/456754567.htm/531613490.htm?menu=contact' as Joseph Silber's solution does.
<?php
$path = parse_url('http://www.mywebsite/456754567/531613490.htm?menu=contact', PHP_URL_PATH);
$pathParts = explode('/', $path);
$fullFilename = array_pop($pathParts);
// better use something like lastIndexOf(), this won't fail for 'xxx.abc.htm'
$filename = substr($fullFilename, 0, strpos($fullFilename, '.'));
var_dump( $filename );
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 an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}
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);