Have a url kind of this
https://example.com.ua/part1/part2/part3/product-123.html
How to get product-123.html with regular expression?
I tried this:
echo
preg_replace('#/([a-zA-Z0-9_-]+\.html)$#','$1','https://example.com.ua/part1/part2/part3/product-123.html');
You don't need regular expression for that
There's more than one way of doing it.
Look into parse_url().
parse_url(): This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
That will get you most of the way there and will also separate the host for you. Then you just explode your way to the last part using explode() and end().
$url = parse_url('http://example.com/project/controller/action/param1/param2');
$url['last'] = end(explode('/', $url[path]));
Array
(
[scheme] => http
[host] => example.com
[path] => /project/controller/action/param1/param2
[last] => param2
)
Or you can go straight to the point like this:
$last = ltrim(strrchr(parse_url($url, PHP_URL_PATH), '/'), '/');
You can also just go a head and use explode() in combination of end() directly on the URL. (it's also a lot shorter if you don't need the extra information of parse_url)
$last = end(explode('/', $url));
You can also just use basename() like this
$url = "http://example.com/project/controller/action/param1/param2";
$last = basename($url);
// Output: param2
The preg_replace only replaces what you have it find. In this case product-123.html. So you're replacing /product-123.html with product-123.html and the https://example.com.ua/part1/part2/part3 remains untouched.
To replace everything and only keep the match you'd do
echo
preg_replace('#.*/([a-zA-Z0-9_-]+\.html)$#','$1','https://example.com.ua/part1/part2/part3/product-123.html');
you don't need a regex though to accomplish this task, and if you did it'd probably be cleaner to use preg_match.
Here's a preg_match approach:
preg_match('#[a-zA-Z0-9_-]+\.html$#', 'https://example.com.ua/part1/part2/part3/product-123.html', $match);
echo $match[0];
Demo: https://3v4l.org/4o9RM
Regex demo: https://regex101.com/r/6dytu0/2/
Why regex?
$str ="https://example.com.ua/part1/part2/part3/product-123.html";
Echo Substr($str, strrpos($str, "/")+1);
https://3v4l.org/rJiGL
Strrpos finds the last / and returns position.
Here is a preg_replace that will work if you must use regex.
https://regex101.com/r/6zJwBo/1
$re = '/.*\//';
$str = 'https://example.com.ua/part1/part2/part3/product-123.html';
$subst = '';
$result = preg_replace($re, $subst, $str);
Related
I have one of the following strings:
mystring
/mystring
mystring?test
/mystring?test
That is one string preceded by one optional / and followed by and optional ?test
I need to get this variables:
$string = "mystring"
$test = false / true depending if ?test is present
I'm trying to use regular expressions but I'm having trouble with the right pattern. I'm trying:
\/?(\w+)(\??\w+)
For example, for "mystring", I'm getting this:
Array
(
[0] => /mystring
[1] => mystrin
[2] => g
)
This is a sample code:
<?
echo "<pre>";
$input = "/mystring";
$pattern = "/\/?(\w+)(\??\w+)/";
$matches = null;
preg_match($pattern, $input, $matches);
print_r($matches);
?>
For a non-regex alternative if you're interested, you can use parse_url. It accepts partial URLs, and it can parse strings like those.
$components = parse_url($input);
$string is the path with leading slash removed, and $test is the equality of the query to the string 'test'.
$string = trim($components['path'], '/');
$test = isset($components['query']) && $components['query'] == 'test';
You're including the ? in catch expression.
The regex you should use: \/?(\w+)\??(\w+)?
This will use less number of steps (46 compared to 56 of your regex) hence less load on the server too.
Demo: https://regex101.com/r/98QNeh/2
I want to take a string and split it (or explode it) into an array by full-stops (periods).
I used to have:
$processed_data = explode(".", $raw_data);
but this removes the full-stop.
Researching, I found preg_split, so tried:
$processed_data = preg_split('\.', $raw_data, PREG_SPLIT_DELIM_CAPTURE);
with both \. and \\.
but try as I might, I cannot find a way to properly include the full-stop.
Would anyone know the right way to do this?
The expected result is:
The string
$raw_data = 'This is my house. This is my car. This is my dog.';
Is broken into an array by full-stop, eg:
array("This is my house.", "This is my car.", "This is my dog.")
To split a string into sentences:
preg_match_all('~\s*\K[^.!?]*[.!?]+~', $raw_data, $matches);
$processed_data = $matches[0];
Note: if you want to handle edge cases like abbreviations, a simple regex doesn't suffice, you need to use nltk or any other nlp tool with a dictionary.
Can you try this.
$string = preg_replace("/\.\s?([A-Z])/", "*****$1", $raw_data);
$array = explode("*****", $string);
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));
Using PHP I have an array that return this data for an image:
Array
(
[0] => http://website.dev/2014/05/my-file-name-here-710x557.png
[1] => 710
[2] => 557
[3] => 1
)
Based on the demo data above, I need to somehow turn this image URL into:
http://website.dev/2014/05/my-file-name-here.png removing the -710x557 from the string.
Some things to keep in mind are:
The file extension can change and be any type of file type
710x557 might not ALWAYS be a 3 digit x 3 digit number. It could be 2 or 4
The reason I mention this is to show I cannot simply use PHP's string functions to remove the last 12 characters in the string and then add the file extension back because the last string characters could possibly be between 10 and 14 characters long sometimes and not always 12.
I was hoping to avoid a heavy regular expression code but if that is the only or best way here then I say go with it.
How do I write a regex that removes the end of a string that could have a varying length in PHP?
You can use a regex like this:
-\d+x\d+(\.\w+)$
Working demo
The code you can use is:
$re = "/-\\d+x\\d+(\\.\\w+)$/";
$str = "http://website.dev/2014/05/my-file-name-here-710x557.png";
$subst = '\1';
$result = preg_replace($re, $subst, $str, 1);
The idea is to match the resolution -NumbersXNumbers using -\d+x\d+ (that we'll get rid of it) and then capture the file extension by using (\.\w+)$ using capturing group. Check the substitution section above.
As long as it is 2 sets of digits with an 'x' in the middle preceded by a dash you can use this regex:
-[\d]*x[\d]*
$string = 'http://website.dev/2014/05/my-file-name-here-710x557.png';
$pattern = '/-[\d]*x[\d]*/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
http://phpfiddle.org/lite/code/eh40-6d1x
You can probably use strrpos in the following manner to do this:
$str = substr($str, 0, strrpos($str, '-')) . substr($str, strrpos($str, '.'));
You can use this regex based code:
$str = "http://website.dev/2014/05/my-file-name-here-710x557.png";
$re = '/-([^-]+)(?=\.[^-]*$)/';
$result = preg_replace($re, '', $str, 1);
//=> http://website.dev/2014/05/my-file-name-here.png
RegEx Demo
$newsrc = preg_replace('#\-\d+x\d+(\.\w+$)#', '$1', $arr[0]);
see http://ideone.com/ZsELQ0
hello here is my html :
<div>
hello.domain.com
holla.domain.com
stack.domain.com
overflow.domain.com </div>
I want to return an array with : hello, holla, stack,overflow
then I have this https://hello.domain.com/c/mark?lang=fr
I want to return the value : mark
I know it should be done with regular expressions. As long as I know how to do it regular expression or not it will be good. thank you
Part 1: Subdomains
$regex = '~\w+(?=\.domain\.com)~i';
preg_match_all($regex, $yourstring, $matches);
print_r($matches[0]);
See the matches in the regex demo.
Match Array:
[0] => hello
[1] => holla
[2] => stack
[3] => overflow
Explanation
The i modifier makes it case-insensitive
\w+ matches letters, digits or underscores (our match)
The lookahead (?=\.domain\.com) asserts that it is followed by .domain.com
Part 2: Substring
$regex = '~https://hello\.domain\.com/c/\K[^\s#?]+(?=\?)~';
if (preg_match($regex, $yourstring, $m)) {
$thematch = $m[0];
}
else { // no match...
}
See the match in the regex demo.
Explanation
https://hello\.domain\.com/c/ matches https://hello.domain.com/c/
The \K tells the engine to drop what was matched so far from the final match it returns
[^\s#?]+ matches any chars that are not a white-space char, ? or # url fragment marker
The lookahead (?=\?) asserts that it is followed by a ?
Although I am not sure where you are trying to take this.
$input = 'somthing.domain.com';
$string = trim($input, '.domain.com');
may help you.
About the second part of your question, you can use the parse_url function:
$yourURL = 'https://hello.domain.com/c/mark?lang=fr';
$result = end(explode('/', parse_url($yourURL, PHP_URL_PATH)));
For the second part of your question (extract part of a URL) others have answered with a highly specific regex solution. More generally what you are trying to do is parse a URL for which there already exists the parse_url() function. You will find the following more flexible and applicable to other URLs:
php > $url = 'https://hello.domain.com/c/mark?lang=fr';
php > $urlpath = parse_url($url, PHP_URL_PATH);
php > print $urlpath ."\n";
/c/mark
php > print basename($urlpath) . "\n";
mark
php > $url = 'ftp://some.where.com.au/abcd/efg/wow?lang=id&q=blah';
php > print basename(parse_url($url, PHP_URL_PATH)) . "\n";
This assumes that you are after the last part of the URL path, but you could use explode("/", $urlpath) to access other components in the path.