How to get specific substring digit from string using php? [duplicate] - php

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
$fileName = "Backup_1486874341.tar.gz"
Here is my file name. How can i get only timestamp from that file name Like 1486874341

Use simple regex in preg_match() to select target digit in file name.
$fileName = "Backup_1486874341.tar.gz";
preg_match("/_(\d+)./", $fileName, $matches);
echo $matches[1];
See result in demo

Here is the sample snippet:
<?php
$str = 'Backup_1486874341.tar.gz';
preg_match_all('!\d+!', $str, $matches);
print_r($matches[0][0]);
?>

Related

Check if string contains word (not a sentence string) [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'm writing a PhP script, but for a filter I need to search for a string in a string.
this is the script I have at the moment:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
// do the magic stuff over here
?>
How do i get the script to search for '$searchfor' in '$stringtocheck'?
Use strpos for to find the needle-string in the haystack-string. And here the haystack-string is stringtocheck and the needle-string is searchfor.
Example of using the function:
<?php
$stringtocheck = "Schiphol-Almere C./Hilversum/Utrecht C.";
$searchfor = "Hilversum";
if(strpos($stringtocheck, $searchfor) !== false)
echo true;
?>

PHP ltrim() not working as expected [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);

How to get string before last slash and after second last slash in URL [duplicate]

This question already has answers here:
Get Last Part of URL PHP
(14 answers)
Closed 1 year ago.
If I have a URL look like this:
http://wwww.example/test1/test2/test3/
How can I retrieve string test3 from the url above?
$str = explode("/","http://wwww.example/test1/test2/test3/");
echo $str[count($str)-2];
DEMO: https://eval.in/83914
Regular expression solution.
$url = 'http://wwww.example/test1/test2/test3/';
preg_match('#.*/(.*)/$#', $url, $matches);
echo $matches[1];
Demo
Using capturing group, $:
preg_match('!/([^/]+)/[^/]*$!', 'http://wwww.example/test1/test2/test3/', $matches);
echo $matches[1];
DEMO: http://ideone.com/7EVfZa
So basename() does exactly that.
$url = 'http://www.example/test1/test2/test3/';
echo basename($url); // returns : test3;

PHP - preg_match for end of filename containing variable numbers [duplicate]

This question already has answers here:
Remove resolution string from image url in PHP
(4 answers)
Closed 9 years ago.
Regular expressions are a bit of a challenge for me. My goal is to determine whether or not a filename ends with this:
_100x200.jpg
where 100 and 200 could be any integer of any number of digits.
So what I'm looking for is a way to match these filenames:
photo_1x3.jpg
abc_100x100.jpg
a file name_50x2000.jpg
Could anybody help me out?
Thanks!
You can use this regex:
/_\d+x\d+\.jpg$/i
Using inside your code you can do:
if (preg_match('/_\d+x\d+\.jpg$/', $image, $arr)) {
// matched
var_dump($arr);
}
You can use preg_match to both check and get your dimensions:
<?php
$str = array(
'photo_1x3.jpg',
'abc_100x100.jpg',
'a file name_50x2000.jpg'
);
foreach($str as $s) {
$match = preg_match( '/_([0-9]+)x([0-9]+)/', $s, $matches);
echo $match ? 'exists' : 'doesn\'t exist';
print_r($matches);
}
?>
Demo: https://eval.in/65623

PHP Preg_Match Extract Comma Seperated Values from String [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP preg_match_all: Extract comma seperated list
I have a string that looks like: [something ="1,2,3"]
I need get everything between the quotes into a string. I'm sure that preg_match is the way to do this but i'm not sure of the expression to use.
$content = [something = "1,2,3"];
$new_string = preg_match('?regex?','$content');
Try this:
$content = '[something = "1,2,3"]';
if (preg_match('/"([^"]+)"/', $content, $matches)) {
$matchedContent = $matches[1];
}

Categories