I try to delete /en. It works well:
$url = "myDomain/en/firstFolder/secondFolder/file.php";
echo str_replace("/en","",$url);
My problem comes when it could be: /en or /fr. I mean $url could be:
"myDomain/en/firstFolder/secondFolder/file.php";
"myDomain/fr/firstFolder/secondFolder/file.php";
I cannot know if I have /en or /fr. How to delete in both cases?
I tried and of course, it does not work:
echo str_replace("/en|/fr","",$url);
str_replace() is able to accept an array as the first two arguments, for example (from the Docs):
search The value being searched for, otherwise known as the needle.
An array may be used to designate multiple needles.
You can use:
echo str_replace( array('/en', '/fr'), '', $url );
Fiddle Demo
str_replace is used to replace string, If you want to use pattern then use preg_replace
$url = "myDomain/fr/firstFolder/secondFolder/file.php";
echo preg_replace('/\/en|\/fr/','',$url);
If you can't be bothered with making an array of the replacements, use a regular expression:
echo preg_replace('#/(en|fr|es|ru|mm)#', '', $url);
...and you might want to do the following to make sure you don't accidentally trim too much:
echo preg_replace('#/(en|fr|es|ru|mm|fi)/#', '/', $url);
Say your language was Finnish ('/fi') -- you would also effectively get rid of the beginning of '/firstFolder', unless you matched /fi/ with leading and trailing slashes, and substituted it with a single slash. If you can't be bothered enumerating all the languages, you can also simply...
echo preg_replace('#myDomain/[a-z]{2}/#', 'myDomain/', $url);
...which seems like the safest and most elegant way to get there.
Related
I have this link, and i need to work only with the numbers from that link.
How would i extract them?
I didn't find any answer that would work with codepcetion.
https://www.my-website.com/de/booking/extras#tab-nav-extras-1426
I tired something like this.
$I->grabFromCurrentUrl('\d+');
But i won't work.
Any ideas ?
Staying within the framework:
The manual clearly says that:
grabFromCurrentUrl
Executes the given regular expression against the current URI and
returns the first capturing group. If no parameters are provided, the
full URI is returned.
Since you didn't used any capturing groups (...), nothing is returned.
Try this:
$I->grabFromCurrentUrl('~(\d+)$~');
The $ at the end is optional, it just states that the string should end with the pattern.
Also note that the opening and closing pattern delimiters you would normally use (/) are replaced by tilde (~) characters for convenience, since the input string has a great chance to contain multiple forward slashes. Custom pattern delimiters are completely standard in regexp, as #Naktibalda pointed it out in this answer.
You can use parse_url() to parse entire URL and then extract the part which is most interested for you. After that you can use regex to extract only numbers from the string.
$url = "https://www.my-website.com/de/booking/extras#tab-nav-extras-1426";
$parsedUrl = parse_url($url);
$fragment = $parsedUrl['fragment']; // Contains: tab-nav-extras-1426
$id = preg_replace('/[^0-9]/', '', $fragment);
var_dump($id); // Output: string(4) "1426"
A variant using preg_match() after parse_url():
$url = "https://www.my-website.com/de/booking/extras#tab-nav-extras-1426";
preg_match('/\d+$/', parse_url($url)['fragment'], $id);
var_dump($id[0]);
// Outputs: string(4) "1426"
I have gotten a page php with this line:
$url = file_get_contents('http://web.com/rss.php');
Now I want replace this:
<link>http://web.com/download/45212/lorem-ipsum</link>
<link>http://web.com/download/34210/dolor-sit</link>
<link>http://web.com/download/78954/consectetur-adipiscing</link>
<link>http://web.com/download/77741/laboris-nisi</link>...
With this:
<link>http://otherweb.com/get-d/45212</link>
<link>http://otherweb.com/get-d/34210</link>
<link>http://otherweb.com/get-d/78954</link>
<link>http://otherweb.com/get-d/77741</link>...
I have replaced a part with str_replace but I don't know to replace the other part.
This is what i have done for the moment:
$url = str_replace('<link>http://web.com/download/','<link>http://otherweb.com/get-d/', $url);
You can do this all with a single line of regex :)
Regex
The below regex will detect your middle numbered section....
<link>http:\/\/web\.com\/download\/(.*?)\/.*?<\/link>
PHP
To use this inside PHP you could use this line of code
$url = preg_replace("/<link>http:\/\/web\.com\/download\/(.*?)\/.*?<\/link>/m", "<link>http://otherweb.com/get-d/$1</link>", $url);
This should do exactly what you need!
Explanation
The way it works is preg_replace looks for <link>http://web.com/download/ at the start and /{something}</link> at the end. It captures the middle area into $1
So when we run preg_replace ($pattern, $replacement, $subject) we tell PHP to just find that middle part (the numbers in your URLS) and embed them into "<link>http://otherweb.com/get-d/$1</link>".
I tested it and it seems to be working :)
Edit: I would propose this answer as best for you as it does everything with a single line, and does not require any str_replace. My answer also will function even if the middle section is alphanumeric, and not only if it is numeric.
All you want to do is:
extract the relevant data e.g. the five digit number
put the extracted part into a new context
$input = 'http://web.com/download/45212/lorem-ipsum';
echo preg_replace('/.*\/(\d+).*/', 'http://otherweb.com/get-d/$1', $input);
To extract the relevant part, you can use (\d+) which means: find one or more digits, the parentheses make this a matching group, so you can access this value via $1.
To match and replace the whole line, you have to augment the pattern with .* (which means, find any number of any character) before and after the (\d+) part.
With this set up, the whole string matches, so the whole string will be replaced.
You should replace the initial part of link with a token, and then preg_replace the end of your string searching for the first / and replacing with the </link>. And so you replace your token with the initial part you desire.
$url = str_replace('<link>http://web.com/download/','init', $url);
$url = preg_replace("/\/.+/", "</link>", $url);
$url = str_replace('init', '<link>http://otherweb.com/get-d/', $url);
You're just missing a simple regex to clean up the last part.
Here's how I did it:
$messed_up = '
<link>http://web.com/download/45212/lorem-ipsum</link>
<link>http://web.com/download/34210/dolor-sit</link>
<link>http://web.com/download/78954/consectetur-adipiscing</link>
<link>http://web.com/download/77741/laboris-nisi</link>';
// Firstly we can clean up the first part (like you did) with str_replace
$clean = str_replace('web.com/download/','otherweb.com/get-d/', $messed_up);
// After that we'll use preg_replace to get rid of the last part
$clean = preg_replace("/(.+\/\d+)\/.*(<.*)/", "$1$2", $clean);
printf($clean);
/* Returns:
<link>http://otherweb.com/get-d/4521</link>
<link>http://otherweb.com/get-d/3421</link>
<link>http://otherweb.com/get-d/7895</link>
<link>http://otherweb.com/get-d/7774</link>
*/
I made this quickly so there might be some room for improvement but it definitely works.
You can check out the code in practice HERE.
If you're interested in learning PHP RegEx This is a great place to practice.
I tend to lose track when I'm dealing with alot of special characters.
I have some urls (badly formatted, not consistent enough to use parse_url), and I want to replace all occurrences of the parameters ?dead and/or ?dead=some_text_here with nothing.
There might be other variables before and after.
Example urls:
http://www.url.com/?dead?dead=whatever_text&wow=test
http://www.url.com/?hello?dead=whatever_text
This is what I thought would work, but it doesnt.
$parsed_url = preg_replace("/(\?dead(?:=.*?)?)(?:\&|$|\?)/", "", $url);
What its supposed to do is check for "?dead", with an optional =value behind, then replace that with nothing. But this is also replacing the ? and &, if there is a parameter following the ?dead parameter. Also its only replacing 1 occurrence, not all.
It makes
http://www.url.com/?dead?dead=whatever_text&wow=test
Become
http://www.url.com/dead=whatever_text&wow=test
I think you want something like this pattern?
(\?dead(=[^&]*|))*
PHP Code:
echo preg_replace('/(\?dead(=[^&]*|))*/','',$sourcestring);
This will produce this output of your given urls:
http://www.url.com/&wow=test
http://www.url.com/&wow=test
http://www.url.com/?hello
You can use \Q and \E (as in QuotE) when dealing with lots of special characters.
The text between these delimiters will be treated literally.
How about:
$parsed_url = preg_replace("/\?dead(?:=[^&?]*)?/", "", $url);
preg_replace should match all occurances by default (the $limit parameter is -1 by default).
This regex is very similar to yours but instead of .* it uses [^&?]* to match until the next ? or &
Hi I have a Link like this:
mypage.php?product=3&page=1
I want to delete the &page=1, &page=2, &page=5 and so.
I have tried this, but I think it is not right.
str_replace('/(\\?|&)page=.*?(&|$)/', '', $link);
Thanks for your help.
str_replace() doesn't work with regular expressions, so you'd use preg_replace() instead:
$url = preg_replace('/[?&]page=[^&]+/', '', $url);
Two changes here: first, it's better to use a character class instead of alternation when you target individual symbol only (not having to escape ? within the brackets is a nice bonus), second, [^&]+ ('match any number of non-& characters') construct is more direct and readable than .+?(&|$) one.
I am looking for a way to get a valid url out of a string like:
$string = 'http://somesite.com/directory//sites/9/my_forms/3-895a3e/somefilename.jpg|:||:||:||:|19845';
My original solution was:
preg_match('#^[^:|]*#', str_replace('//', '/', $string), $modifiedPath);
But obviously its going to remove a slash from the http:// instead of the one in the middle of the string.
My expected output that I want from the original is:
http://somesite.com/directory/sites/9/my_forms/3-895a3e/somefilename.jpg
I could always break off the http part of the string first but would like a more elegant solution in the form of regex if possible. Thanks.
This will do exactly what you are asking:
<?php
$string = 'http://somesite.com/directory//sites/9/my_forms/3-895a3e/somefilename.jpg|:||:||:||:|19845';
preg_match('/^([^|]+)/', $string, $m); // get everything up to and NOT including the first pipe (|)
$string = $m[1];
$string = preg_replace('/(?<!:)\/\//', '/' ,$string); // replace all occurrences of // as long as they are not preceded by :
echo $string; // outputs: http://somesite.com/directory/sites/9/my_forms/3-895a3e/somefilename.jpg
exit;
?>
EDIT:
(?<!X) in regular expressions is the syntax for what is called a lookbehind. The X is replaced with the character(s) we are testing for.
The following expression would match every instance of double slashes (/):
\/\/
But we need to make sure that the match we are looking for is NOT preceded by the : character so we need to 'lookbehind' our match to see if the : character is there. If it is then we don't want it to be counted as a match:
(?<!:)\/\/
The ! is what says NOT to match in our lookbehind. If we changed it to (?=:)\/\/ then it would only match the double slashes that did have the : preceding them.
Here is a Quick tutorial that can explain it all better than I can lookahead and lookbehind tutorial
Assuming all your strings are in the form given, you don't need any but the simplest of regexes to do this; if you want an elegant solution, then a regex is definitely not what you need. Also, double slashes are legal in a URL, just like in a Unix path, and mean the same thing a single slash does, so you don't really need to get rid of them at all.
Why not just
$url = array_shift(preg_split('/\|/', $string));
?
If you really, really care about getting rid of the double slashes in the URL, then you can follow this with
$url = preg_replace('/([^:])\/\//', '$1/', $url);
or even combine them into
$url = preg_replace('/([^:])\/\//', '$1/', array_shift(preg_split('/\|/', $string)));
although that last form gets a little bit hairy.
Since this is a quite strictly defined situation, I'd consider just one preg to be the most elegant solution.
From the top of my head:
$sanitizedURL = preg_replace('~((?<!:)/(?=/)|\\|.+)~', '', $rawURL);
Basically, what this does is look for any forward slash that IS NOT preceded by a colon (:), and IS followed bij another forward slash. It also searches for any pipe character and any character following it.
Anything found is removed from the result.
I can explain the RegEx in more detail if you like.