Replace first path of URL with new variable in PHP












0















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?










share|improve this question

























  • 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
















0















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?










share|improve this question

























  • 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














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












2 Answers
2






active

oldest

votes


















1














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]";
}





share|improve this answer































    0














    $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






    share|improve this answer


























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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]";
    }





    share|improve this answer




























      1














      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]";
      }





      share|improve this answer


























        1












        1








        1







        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]";
        }





        share|improve this answer













        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]";
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 2:09









        miken32miken32

        23.9k84972




        23.9k84972

























            0














            $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






            share|improve this answer


























            • 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
















            0














            $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






            share|improve this answer


























            • 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














            0












            0








            0







            $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






            share|improve this answer















            $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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)