I am trying to extract the last word of a string but ignoring any extension it may have
e.g. amazon_uk instead of amazon_uk.gif
The following code extracts the word from the string using 2 preg_match functions, I want to be able to do the same thing in 1 preg_match, how can I do this?
php code
$str = 'http://i.example.com/about/bs/logo_borderless/amazon_uk.gif';
preg_match('/[^\.\/]+\.[^\.\/]+$/', $str, $matches);
preg_match('/^[^.]+(?=.)/', $matches[0], $matches2);
$website = $matches2[0];
output
amazon_uk
preg_match( '#/([^./]+)\.[^./]+$#si', $str, $matches );
Here's what it's doing...
/
match a forward slash
([^./]+)
Then one or more of neither a period or forward slash. This is the bit we're matching.
\.
Then a period
[^./]+
Then one or more of neither a period or forward slash again.
$
Then the end of the string
You asked about a regex, so that's above. But here's what I'd actually do...
$url = 'http://i.example.com/about/bs/logo_borderless/amazon_uk.gif';
$output = str_replace( array('.gif','.jpg','.png'), '', basename($url) );
Basename's something I use all the time - very handy.
Because it will always be in the format you specified (per a comment), you can also use a combination of substr() and strpos() (and strrpos()) to get the text as opposed to regex:
// get the filename after the last slash
$file = substr($str, strrpos($str, '/') + 1);
// get the text before the extension
$website = substr($file, 0, strpos($file, '.'));
preg_match('/\/([\w]+)\.(?:[a-zA-Z]{1,3})$/', $str, $matches);
$result = $matches[1];
A non-greedy search plus a optional match on the extension should do the trick:
preg_match('/([^\.\/]+?)(?:\.\w*)?$/', $str, $matches);
$website = $matches[1];
Related
I have some string, for example:
cats, e.g. Barsik, are funny. And it is true. So,
And I want to get as result:
cats, e.g. Barsik, are funny.
My try:
mb_ereg_search_init($text, '((?!e\.g\.).)*\.[^\.]');
$match = mb_ereg_search_pos();
But it gets position of second dot (after word "true").
How to get desired result?
Since a naive approach works for you, I am posting an answer. However, please note that detecting a sentence end is a very difficult task for a regex, and although it is possible to some degree, an NLP package should be used for that.
Having said that, I suggested using
'~(?<!\be\.g)\.(?=\s+\p{Lu})~ui'
The regex matches any dot (\.) that is not preceded with a whole word e.g (see the negative lookbehind (?<!\be\.g)), but that is followed with 1 or more whitespaces (\s+) followed with 1 uppercase Unicode letter \p{Lu}.
See the regex demo
The case insensitive i modifier does not impact what \p{Lu} matches.
The ~u modifier is required since you are working with Unicode texts (like Russian).
To get the index of the first occurrence, use a preg_match function with the PREG_OFFSET_CAPTURE flag. Here is a bit simplified regex you supplied in the comments:
preg_match('~(?<!т\.н)(?<!т\.к)(?<!e\.g)\.(?=\s+\p{L})~iu', $text, $match, PREG_OFFSET_CAPTURE);
See the lookaheads are executed one by one, and at the same location in string, thus, you do not have to additionally group them inside a positive lookahead. See the regex demo.
IDEONE demo:
$re = '~(?<!т\.н)(?<!т\.к)(?<!e\.g)\.(?=\s+\p{L})~iu';
$str = "cats, e.g. Barsik, are funny. And it is true. So,";
preg_match($re, $str, $match, PREG_OFFSET_CAPTURE);
echo $match[0][1];
Here are two approaches to get substring from start to second last . position of the initial string:
using strrpos and substr functions:
$str = 'cats, e.g. Barsik, and e.g. Lusya are funny. And it is true. So,';
$len = strlen($str);
$str = substr($str, 0, (strrpos($str, '.', strrpos($str, '.') - $len - 1) - $len) + 1);
print_r($str); // "cats, e.g. Barsik, and e.g. Lusya are funny."
using array_reverse, str_split and array_search functions:
$str = 'cats, e.g. Barsik, and e.g. Lusya are funny. And it is true. So,';
$parts = array_reverse(str_split($str));
$pos = array_search('.', $parts) + 1;
$str = implode("", array_reverse(array_slice($parts, array_search('.', array_slice($parts, $pos)) + $pos)));
print_r($str); // "cats, e.g. Barsik, and e.g. Lusya are funny."
I would like to know how I can cut a string in PHP starting from the last character -> to a specific character. Lets say I have following link:
www.whatever.com/url/otherurl/2535834
and I want to get 2535834
Important note: the number can have a different length, which is why I want to cut out to the / no matter how many numbers there are.
Thanks
In this special case, an url, use basename() :
echo basename('www.whatever.com/url/otherurl/2535834');
A more general solution would be preg_replace(), like this:
<----- the delimiter which separates the search string from the remaining part of the string
echo preg_replace('#.*/#', '', $url);
The pattern '#.*/#' makes usage of the default greediness of the PCRE regex engine - meaning it will match as many chars as possible and will therefore consume /abc/123/xyz/ instead of just /abc/ when matching the pattern.
Use
explode() AND end()
<?php
$str = 'www.whatever.com/url/otherurl/2535834';
$tmp = explode('/', $str);
echo end ($tmp);
?>
Working Demo
This should work for you:
(So you can get the number with or without a slash, if you need that)
<?php
$url = "www.whatever.com/url/otherurl/2535834";
preg_match("/\/(\d+)$/",$url,$matches);
print_r($matches);
?>
Output:
Array ( [0] => /2535834 [1] => 2535834 )
With strstr() and str_replace() in action
$str = 'www.whatever.com/url/otherurl/2535834';
echo str_replace("otherurl/", "", strstr($str, "otherurl/"));
strstr() finds everything (including the needle) after the needle and the needle gets replaced by "" using str_replace()
if your pattern is fixed you can always do:
$str = 'www.whatever.com/url/otherurl/2535834';
$tmp = explode('/', $str);
echo $temp[3];
Here's mine version:
$string = "www.whatever.com/url/otherurl/2535834";
echo substr($string, strrpos($string, "/") + 1, strlen($string));
I have a query string stored in a variable and I need to strip out some stuff from it using preg_replace()
the parameters I want to strip out look like this:
&filtered_features[48][]=491
As there will be multiples of these parameters in the query string the 48 and the 491 can be any number so the regex needs to essentially match this:
'&filtered_features[' + Any number + '][]=' + Any number
Anyone know how I would do this?
$string = '&filtered_features[48][]=491';
$string = preg_replace('/\[\d+\]\[\]=\d+/', '[][]=', $string);
echo $string;
I assume you wanted to remove the numbers from the string. This will match a multi-variable query string as well since it just looks for [A_NUMBER][]=A_NUMBER and changes it to [][]=
$query_string = "&filtered_features[48][]=491&filtered_features[49][]=492";
$lines = explode("&", $query_string);
$pattern = "/filtered_features\[([0-9]*)\]\[\]=([0-9]*)/";
foreach($lines as $line)
{
preg_match($pattern, $line, $m);
var_dump($m);
}
/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/'
this will match first number in n1 and second in n2
preg_match_all( '/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/', $str, $matches);
cryptic answer will replace more than necessary with this string:
&something[1][]=123&filtered_features[48][]=491
I'm trying to use PHP regular expressions. I've tried this code:
$regex = "c:(.+),";
$input = "otherStuff094322f98c:THIS,OtherStuffHeree129j12dls";
$match = Array();
preg_match_all($regex, $input, $match);
It should return a sub-string THIS ("c" and ":" followed by any character combination followed by ",") from $input. But it returns a empty array. What am I doing wrong?
I think you need the slashes to make regex working.
and using .+ will match everything behind the comma too, which is you don't want. Use .+? or [^,]+
$regex = "/c:(.+?),/";
or
$regex = "/c:([^,]+),/";
Does anyone know how to remove the first few characters from a string and remove them in PHP.
Like in the string "str_filename" I need to remove the "str_" and save the "filename".
But it has to remove as many charactors as it takes to get to the "_".
In other words, i need to remove all the characters up until and including the first "_" in the string.
You can do this:
if (strpos($string, '_') !== false)
$string = substr($string, strpos($string, '_') + 1);
It works as you can see here: http://codepad.org/g12ENLGY
Note: The if is useful because your string could not have the '_' char.
Could you use something like:
$files = explode("_", $filename);
echo $files[1];
So this would split the string on the _ character, and then echo the second part (after the underscore).
This assumes that there is only one underscore though.
Please see http://www.php.net/manual/en/function.explode.php
An additional solution than using explode and substr as have been mentioned you can solve it using regex:
^(?:[^_]*_)(.*)$
Here's an example using it:
$str = "str_filename";
$pattern = "/^(?:[^_]*_)(.*)$/";
preg_match($pattern, $str, $matches);
echo $matches[1]; //prints "filename"