Replace first path of URL with new variable in PHP
Is it possible to, for example, take this URL:
https://www.example.com/en/blah/blah
But replace "en" with a passed variable?
The PHP I have: (below returns URL little like above)
'href' => $this->url->link($route, $url)
I have the new language variable available with
$result['url_code']
But not had any luck with replacing the returned link with replacing the first url section with the result variable.
The initial returned URL with the /*/ first part of the url path will change depending on language so need to replace anything in that first part with the variable $result['url_code']
Any ideas?
php
add a comment |
Is it possible to, for example, take this URL:
https://www.example.com/en/blah/blah
But replace "en" with a passed variable?
The PHP I have: (below returns URL little like above)
'href' => $this->url->link($route, $url)
I have the new language variable available with
$result['url_code']
But not had any luck with replacing the returned link with replacing the first url section with the result variable.
The initial returned URL with the /*/ first part of the url path will change depending on language so need to replace anything in that first part with the variable $result['url_code']
Any ideas?
php
what have you tried so far?
– Dekel
Nov 20 '18 at 1:57
been playing with str_replace and also http_build_query but not having much luck lol
– James
Nov 20 '18 at 1:59
1
1. add some code that you tried. 2. check onexplode
– Dekel
Nov 20 '18 at 2:01
1
Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 '18 at 21:42
I am aware of marking questions / upvoting etc.. if you see from all my questions i always do this however the solution was not working right for me and awaiting a reply from my comments.
– James
Dec 5 '18 at 1:31
add a comment |
Is it possible to, for example, take this URL:
https://www.example.com/en/blah/blah
But replace "en" with a passed variable?
The PHP I have: (below returns URL little like above)
'href' => $this->url->link($route, $url)
I have the new language variable available with
$result['url_code']
But not had any luck with replacing the returned link with replacing the first url section with the result variable.
The initial returned URL with the /*/ first part of the url path will change depending on language so need to replace anything in that first part with the variable $result['url_code']
Any ideas?
php
Is it possible to, for example, take this URL:
https://www.example.com/en/blah/blah
But replace "en" with a passed variable?
The PHP I have: (below returns URL little like above)
'href' => $this->url->link($route, $url)
I have the new language variable available with
$result['url_code']
But not had any luck with replacing the returned link with replacing the first url section with the result variable.
The initial returned URL with the /*/ first part of the url path will change depending on language so need to replace anything in that first part with the variable $result['url_code']
Any ideas?
php
php
edited Nov 29 '18 at 0:00
halfer
14.6k758112
14.6k758112
asked Nov 20 '18 at 1:56
JamesJames
8971022
8971022
what have you tried so far?
– Dekel
Nov 20 '18 at 1:57
been playing with str_replace and also http_build_query but not having much luck lol
– James
Nov 20 '18 at 1:59
1
1. add some code that you tried. 2. check onexplode
– Dekel
Nov 20 '18 at 2:01
1
Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 '18 at 21:42
I am aware of marking questions / upvoting etc.. if you see from all my questions i always do this however the solution was not working right for me and awaiting a reply from my comments.
– James
Dec 5 '18 at 1:31
add a comment |
what have you tried so far?
– Dekel
Nov 20 '18 at 1:57
been playing with str_replace and also http_build_query but not having much luck lol
– James
Nov 20 '18 at 1:59
1
1. add some code that you tried. 2. check onexplode
– Dekel
Nov 20 '18 at 2:01
1
Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 '18 at 21:42
I am aware of marking questions / upvoting etc.. if you see from all my questions i always do this however the solution was not working right for me and awaiting a reply from my comments.
– James
Dec 5 '18 at 1:31
what have you tried so far?
– Dekel
Nov 20 '18 at 1:57
what have you tried so far?
– Dekel
Nov 20 '18 at 1:57
been playing with str_replace and also http_build_query but not having much luck lol
– James
Nov 20 '18 at 1:59
been playing with str_replace and also http_build_query but not having much luck lol
– James
Nov 20 '18 at 1:59
1
1
1. add some code that you tried. 2. check on
explode
– Dekel
Nov 20 '18 at 2:01
1. add some code that you tried. 2. check on
explode
– Dekel
Nov 20 '18 at 2:01
1
1
Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 '18 at 21:42
Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 '18 at 21:42
I am aware of marking questions / upvoting etc.. if you see from all my questions i always do this however the solution was not working right for me and awaiting a reply from my comments.
– James
Dec 5 '18 at 1:31
I am aware of marking questions / upvoting etc.. if you see from all my questions i always do this however the solution was not working right for me and awaiting a reply from my comments.
– James
Dec 5 '18 at 1:31
add a comment |
2 Answers
2
active
oldest
votes
You may want to look at parse_url()
to deconstruct the value that you have, and then rebuild it. This is certainly more verbose than it needs to be, but it does the job reliably and will not break on edge cases.
<?php
$url = $this->url->link($route, $url);
$url_parts = parse_url($url);
$path_parts = explode("/", $url_parts["path"]);
// there's a leading slash, so [0] is an empty string
$path_parts[1] = $result['url_code'];
$path = implode("/", $path_parts);
$new_url = "$url_parts[scheme]://$url_parts[host]$path";
if (!empty($url_parts["query"])) {
$new_url .= "?$url_parts[query]";
}
add a comment |
$result = "Replaced Word" ;
$url = "https://www.example.com/en/blah/blah";
function findFirst($findingWord, $replacingWord, $url) {
return implode($replacingWord, explode($findingWord, $url, 2));
}
echo findFirst("blah",$result, $url)
Before copy this read those
Implode Reference
explode Reference
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385151%2freplace-first-path-of-url-with-new-variable-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may want to look at parse_url()
to deconstruct the value that you have, and then rebuild it. This is certainly more verbose than it needs to be, but it does the job reliably and will not break on edge cases.
<?php
$url = $this->url->link($route, $url);
$url_parts = parse_url($url);
$path_parts = explode("/", $url_parts["path"]);
// there's a leading slash, so [0] is an empty string
$path_parts[1] = $result['url_code'];
$path = implode("/", $path_parts);
$new_url = "$url_parts[scheme]://$url_parts[host]$path";
if (!empty($url_parts["query"])) {
$new_url .= "?$url_parts[query]";
}
add a comment |
You may want to look at parse_url()
to deconstruct the value that you have, and then rebuild it. This is certainly more verbose than it needs to be, but it does the job reliably and will not break on edge cases.
<?php
$url = $this->url->link($route, $url);
$url_parts = parse_url($url);
$path_parts = explode("/", $url_parts["path"]);
// there's a leading slash, so [0] is an empty string
$path_parts[1] = $result['url_code'];
$path = implode("/", $path_parts);
$new_url = "$url_parts[scheme]://$url_parts[host]$path";
if (!empty($url_parts["query"])) {
$new_url .= "?$url_parts[query]";
}
add a comment |
You may want to look at parse_url()
to deconstruct the value that you have, and then rebuild it. This is certainly more verbose than it needs to be, but it does the job reliably and will not break on edge cases.
<?php
$url = $this->url->link($route, $url);
$url_parts = parse_url($url);
$path_parts = explode("/", $url_parts["path"]);
// there's a leading slash, so [0] is an empty string
$path_parts[1] = $result['url_code'];
$path = implode("/", $path_parts);
$new_url = "$url_parts[scheme]://$url_parts[host]$path";
if (!empty($url_parts["query"])) {
$new_url .= "?$url_parts[query]";
}
You may want to look at parse_url()
to deconstruct the value that you have, and then rebuild it. This is certainly more verbose than it needs to be, but it does the job reliably and will not break on edge cases.
<?php
$url = $this->url->link($route, $url);
$url_parts = parse_url($url);
$path_parts = explode("/", $url_parts["path"]);
// there's a leading slash, so [0] is an empty string
$path_parts[1] = $result['url_code'];
$path = implode("/", $path_parts);
$new_url = "$url_parts[scheme]://$url_parts[host]$path";
if (!empty($url_parts["query"])) {
$new_url .= "?$url_parts[query]";
}
answered Nov 20 '18 at 2:09
miken32miken32
23.9k84972
23.9k84972
add a comment |
add a comment |
$result = "Replaced Word" ;
$url = "https://www.example.com/en/blah/blah";
function findFirst($findingWord, $replacingWord, $url) {
return implode($replacingWord, explode($findingWord, $url, 2));
}
echo findFirst("blah",$result, $url)
Before copy this read those
Implode Reference
explode Reference
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
|
show 1 more comment
$result = "Replaced Word" ;
$url = "https://www.example.com/en/blah/blah";
function findFirst($findingWord, $replacingWord, $url) {
return implode($replacingWord, explode($findingWord, $url, 2));
}
echo findFirst("blah",$result, $url)
Before copy this read those
Implode Reference
explode Reference
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
|
show 1 more comment
$result = "Replaced Word" ;
$url = "https://www.example.com/en/blah/blah";
function findFirst($findingWord, $replacingWord, $url) {
return implode($replacingWord, explode($findingWord, $url, 2));
}
echo findFirst("blah",$result, $url)
Before copy this read those
Implode Reference
explode Reference
$result = "Replaced Word" ;
$url = "https://www.example.com/en/blah/blah";
function findFirst($findingWord, $replacingWord, $url) {
return implode($replacingWord, explode($findingWord, $url, 2));
}
echo findFirst("blah",$result, $url)
Before copy this read those
Implode Reference
explode Reference
edited Nov 20 '18 at 2:21
answered Nov 20 '18 at 2:10
Munkhdelger TumenbayarMunkhdelger Tumenbayar
850619
850619
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
|
show 1 more comment
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
Please test your code before posting it.
– Nick
Nov 20 '18 at 2:20
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
@Nick Sorry my bad fixed the problem so could get off your vote from my answer please?
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:22
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
I just need to replace the first part of the path with that variable, it changes you see so cannot really manually do that function to find and replace.... simply want to just replace any content in first part of path with a variable.
– James
Nov 20 '18 at 2:27
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
@James i dont get it your question was string replace on specific string like "example.com/blah/blah" to "example.com/replace/blah" i taking that answering it now it changes? ok
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:38
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
" Is it possible to for example take this URL: example.com/en/blah/blah But replace the first folder level with a passed variable?" bruh
– Munkhdelger Tumenbayar
Nov 20 '18 at 2:39
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385151%2freplace-first-path-of-url-with-new-variable-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what have you tried so far?
– Dekel
Nov 20 '18 at 1:57
been playing with str_replace and also http_build_query but not having much luck lol
– James
Nov 20 '18 at 1:59
1
1. add some code that you tried. 2. check on
explode
– Dekel
Nov 20 '18 at 2:01
1
Were any of the provided answers helpful? You should upvote all answers that were helpful if you have the reputation to do so, and mark accepted the one answer that best answered your question. This will mark the question as "closed," and give you some reputation on the site. See stackoverflow.com/help/someone-answers for more information.
– miken32
Nov 29 '18 at 21:42
I am aware of marking questions / upvoting etc.. if you see from all my questions i always do this however the solution was not working right for me and awaiting a reply from my comments.
– James
Dec 5 '18 at 1:31