Placing text before every a href appearance?











up vote
1
down vote

favorite












I am currently trying to place something like this https://shortdomain.com/api?p=a-href-url-here.com before every link on my website in a specific div.



$main = get_the_content_with_formatting();
$stripped = str_replace('<a href="http://example.net/', '<a href="https://shortdomain.com/api?p=http://example.net/', $main);


The method above works, but only when the link is that URL, and with any other it obviously will just return the standard URL.



Is there a way that I can prefix my desired link to each href using JavaScript, or PHP?



Here's the selector of the contents inside of $main



#the-post > div.post-inner > div.entry > p > strong > a









share|improve this question




















  • 2




    you could use a DOM parser in php
    – IdontDownVote
    Nov 21 at 2:03










  • @IdontDownVote I thought about this, but it seems messy..
    – John Doe
    Nov 21 at 2:04






  • 1




    "Messy?" I'd submit that treating markup language like a plain text string is messy!
    – miken32
    Nov 21 at 2:09

















up vote
1
down vote

favorite












I am currently trying to place something like this https://shortdomain.com/api?p=a-href-url-here.com before every link on my website in a specific div.



$main = get_the_content_with_formatting();
$stripped = str_replace('<a href="http://example.net/', '<a href="https://shortdomain.com/api?p=http://example.net/', $main);


The method above works, but only when the link is that URL, and with any other it obviously will just return the standard URL.



Is there a way that I can prefix my desired link to each href using JavaScript, or PHP?



Here's the selector of the contents inside of $main



#the-post > div.post-inner > div.entry > p > strong > a









share|improve this question




















  • 2




    you could use a DOM parser in php
    – IdontDownVote
    Nov 21 at 2:03










  • @IdontDownVote I thought about this, but it seems messy..
    – John Doe
    Nov 21 at 2:04






  • 1




    "Messy?" I'd submit that treating markup language like a plain text string is messy!
    – miken32
    Nov 21 at 2:09















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am currently trying to place something like this https://shortdomain.com/api?p=a-href-url-here.com before every link on my website in a specific div.



$main = get_the_content_with_formatting();
$stripped = str_replace('<a href="http://example.net/', '<a href="https://shortdomain.com/api?p=http://example.net/', $main);


The method above works, but only when the link is that URL, and with any other it obviously will just return the standard URL.



Is there a way that I can prefix my desired link to each href using JavaScript, or PHP?



Here's the selector of the contents inside of $main



#the-post > div.post-inner > div.entry > p > strong > a









share|improve this question















I am currently trying to place something like this https://shortdomain.com/api?p=a-href-url-here.com before every link on my website in a specific div.



$main = get_the_content_with_formatting();
$stripped = str_replace('<a href="http://example.net/', '<a href="https://shortdomain.com/api?p=http://example.net/', $main);


The method above works, but only when the link is that URL, and with any other it obviously will just return the standard URL.



Is there a way that I can prefix my desired link to each href using JavaScript, or PHP?



Here's the selector of the contents inside of $main



#the-post > div.post-inner > div.entry > p > strong > a






javascript php jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 2:22









miken32

22.6k84571




22.6k84571










asked Nov 21 at 1:52









John Doe

618




618








  • 2




    you could use a DOM parser in php
    – IdontDownVote
    Nov 21 at 2:03










  • @IdontDownVote I thought about this, but it seems messy..
    – John Doe
    Nov 21 at 2:04






  • 1




    "Messy?" I'd submit that treating markup language like a plain text string is messy!
    – miken32
    Nov 21 at 2:09
















  • 2




    you could use a DOM parser in php
    – IdontDownVote
    Nov 21 at 2:03










  • @IdontDownVote I thought about this, but it seems messy..
    – John Doe
    Nov 21 at 2:04






  • 1




    "Messy?" I'd submit that treating markup language like a plain text string is messy!
    – miken32
    Nov 21 at 2:09










2




2




you could use a DOM parser in php
– IdontDownVote
Nov 21 at 2:03




you could use a DOM parser in php
– IdontDownVote
Nov 21 at 2:03












@IdontDownVote I thought about this, but it seems messy..
– John Doe
Nov 21 at 2:04




@IdontDownVote I thought about this, but it seems messy..
– John Doe
Nov 21 at 2:04




1




1




"Messy?" I'd submit that treating markup language like a plain text string is messy!
– miken32
Nov 21 at 2:09






"Messy?" I'd submit that treating markup language like a plain text string is messy!
– miken32
Nov 21 at 2:09














5 Answers
5






active

oldest

votes

















up vote
3
down vote



accepted










Don't ever make the mistake of parsing HTML like text; it's not! Use a proper DOM parser to extract your values and then alter them.



<?php
$main = "<div><p>Here is some <a href='http://example.com/'>sample</a> text.</p></div>";
$dom = new DomDocument();
$dom->loadHtml($main, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
foreach ($dom->getElementsByTagName("a") as $anchor) {
$href = $anchor->getAttribute("href");
if ($href) {
$anchor->setAttribute("href", "https://shortdomain.com/api?p=" . urlencode($href));
}
}
echo $dom->saveHTML();





share|improve this answer





















  • This will change every anchor in the document rather than just those beneath a div with class post-inner
    – Nick
    Nov 21 at 2:14










  • Actually I think he may only be providing part of the document content in $main so this code is ok.
    – miken32
    Nov 21 at 2:19










  • It's hard to say from the question. I think you're probably right.
    – Nick
    Nov 21 at 2:19


















up vote
3
down vote













This is a good task for PHP's built-in DOMDocument and DOMXpath classes. Using xpath ensures that you only change anchors inside the div that you want.



$main = '<div class="post-inner"><div class="entry"><p><strong><a href="http://example.net">link</a></strong></p></div></div>';
$doc = new DOMDocument();
$doc->loadHTML($main, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$anchors = $xpath->query('//div[contains(@class, "post-inner")]//a');
foreach ($anchors as $a) {
$a->setAttribute('href', 'https://shortdomain.com/api?p=' . $a->attributes->getNamedItem('href')->nodeValue);
}
echo $doc->saveHTML();


Output:



<div class="post-inner"><div class="entry"><p><strong>
<a href="https://shortdomain.com/api?p=http://example.net">link</a>
</strong></p></div></div>


Demo on 3v4l.org






share|improve this answer






























    up vote
    1
    down vote













    You can use the attribute selector with jquery to match all desired elements and then use the method 'attr' to change the href attribute.



    $("#the-post > div.post-inner > div.entry > p > strong > a[href="http://example.net/"]")
    .attr("href", "https://shortdomain.com/api?p=http://example.net/");


    I didn't really test it, but it should work just fine.






    share|improve this answer




























      up vote
      1
      down vote













      If you are doing this client-side, it's pretty painless with javascript's querySelectorAll:






      let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
      as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

      <a href="http://example.net/">Don't change</a>
      <div id="the-post">
      <div class="post-inner">
      <div class="entry">
      <p>
      change these:
      <strong>
      <a href="http://example2.net/">some inside </a> <br />
      <a href="http://example3.net/">some other inside </a>
      </strong></p>
      </div>
      </div>
      </div>








      share|improve this answer






























        up vote
        0
        down vote













        Get rid of the dummy website then.






        $main = get_the_content_with_formatting();
        $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);








        share|improve this answer























        • I might be a bit slow for not thinking of this..
          – John Doe
          Nov 21 at 2:11










        • What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
          – miken32
          Nov 21 at 2:13












        • then remove <a
          – ACD
          Nov 21 at 2:25











        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',
        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%2f53404252%2fplacing-text-before-every-a-href-appearance%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        Don't ever make the mistake of parsing HTML like text; it's not! Use a proper DOM parser to extract your values and then alter them.



        <?php
        $main = "<div><p>Here is some <a href='http://example.com/'>sample</a> text.</p></div>";
        $dom = new DomDocument();
        $dom->loadHtml($main, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
        foreach ($dom->getElementsByTagName("a") as $anchor) {
        $href = $anchor->getAttribute("href");
        if ($href) {
        $anchor->setAttribute("href", "https://shortdomain.com/api?p=" . urlencode($href));
        }
        }
        echo $dom->saveHTML();





        share|improve this answer





















        • This will change every anchor in the document rather than just those beneath a div with class post-inner
          – Nick
          Nov 21 at 2:14










        • Actually I think he may only be providing part of the document content in $main so this code is ok.
          – miken32
          Nov 21 at 2:19










        • It's hard to say from the question. I think you're probably right.
          – Nick
          Nov 21 at 2:19















        up vote
        3
        down vote



        accepted










        Don't ever make the mistake of parsing HTML like text; it's not! Use a proper DOM parser to extract your values and then alter them.



        <?php
        $main = "<div><p>Here is some <a href='http://example.com/'>sample</a> text.</p></div>";
        $dom = new DomDocument();
        $dom->loadHtml($main, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
        foreach ($dom->getElementsByTagName("a") as $anchor) {
        $href = $anchor->getAttribute("href");
        if ($href) {
        $anchor->setAttribute("href", "https://shortdomain.com/api?p=" . urlencode($href));
        }
        }
        echo $dom->saveHTML();





        share|improve this answer





















        • This will change every anchor in the document rather than just those beneath a div with class post-inner
          – Nick
          Nov 21 at 2:14










        • Actually I think he may only be providing part of the document content in $main so this code is ok.
          – miken32
          Nov 21 at 2:19










        • It's hard to say from the question. I think you're probably right.
          – Nick
          Nov 21 at 2:19













        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        Don't ever make the mistake of parsing HTML like text; it's not! Use a proper DOM parser to extract your values and then alter them.



        <?php
        $main = "<div><p>Here is some <a href='http://example.com/'>sample</a> text.</p></div>";
        $dom = new DomDocument();
        $dom->loadHtml($main, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
        foreach ($dom->getElementsByTagName("a") as $anchor) {
        $href = $anchor->getAttribute("href");
        if ($href) {
        $anchor->setAttribute("href", "https://shortdomain.com/api?p=" . urlencode($href));
        }
        }
        echo $dom->saveHTML();





        share|improve this answer












        Don't ever make the mistake of parsing HTML like text; it's not! Use a proper DOM parser to extract your values and then alter them.



        <?php
        $main = "<div><p>Here is some <a href='http://example.com/'>sample</a> text.</p></div>";
        $dom = new DomDocument();
        $dom->loadHtml($main, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
        foreach ($dom->getElementsByTagName("a") as $anchor) {
        $href = $anchor->getAttribute("href");
        if ($href) {
        $anchor->setAttribute("href", "https://shortdomain.com/api?p=" . urlencode($href));
        }
        }
        echo $dom->saveHTML();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 2:07









        miken32

        22.6k84571




        22.6k84571












        • This will change every anchor in the document rather than just those beneath a div with class post-inner
          – Nick
          Nov 21 at 2:14










        • Actually I think he may only be providing part of the document content in $main so this code is ok.
          – miken32
          Nov 21 at 2:19










        • It's hard to say from the question. I think you're probably right.
          – Nick
          Nov 21 at 2:19


















        • This will change every anchor in the document rather than just those beneath a div with class post-inner
          – Nick
          Nov 21 at 2:14










        • Actually I think he may only be providing part of the document content in $main so this code is ok.
          – miken32
          Nov 21 at 2:19










        • It's hard to say from the question. I think you're probably right.
          – Nick
          Nov 21 at 2:19
















        This will change every anchor in the document rather than just those beneath a div with class post-inner
        – Nick
        Nov 21 at 2:14




        This will change every anchor in the document rather than just those beneath a div with class post-inner
        – Nick
        Nov 21 at 2:14












        Actually I think he may only be providing part of the document content in $main so this code is ok.
        – miken32
        Nov 21 at 2:19




        Actually I think he may only be providing part of the document content in $main so this code is ok.
        – miken32
        Nov 21 at 2:19












        It's hard to say from the question. I think you're probably right.
        – Nick
        Nov 21 at 2:19




        It's hard to say from the question. I think you're probably right.
        – Nick
        Nov 21 at 2:19












        up vote
        3
        down vote













        This is a good task for PHP's built-in DOMDocument and DOMXpath classes. Using xpath ensures that you only change anchors inside the div that you want.



        $main = '<div class="post-inner"><div class="entry"><p><strong><a href="http://example.net">link</a></strong></p></div></div>';
        $doc = new DOMDocument();
        $doc->loadHTML($main, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $xpath = new DOMXpath($doc);
        $anchors = $xpath->query('//div[contains(@class, "post-inner")]//a');
        foreach ($anchors as $a) {
        $a->setAttribute('href', 'https://shortdomain.com/api?p=' . $a->attributes->getNamedItem('href')->nodeValue);
        }
        echo $doc->saveHTML();


        Output:



        <div class="post-inner"><div class="entry"><p><strong>
        <a href="https://shortdomain.com/api?p=http://example.net">link</a>
        </strong></p></div></div>


        Demo on 3v4l.org






        share|improve this answer



























          up vote
          3
          down vote













          This is a good task for PHP's built-in DOMDocument and DOMXpath classes. Using xpath ensures that you only change anchors inside the div that you want.



          $main = '<div class="post-inner"><div class="entry"><p><strong><a href="http://example.net">link</a></strong></p></div></div>';
          $doc = new DOMDocument();
          $doc->loadHTML($main, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
          $xpath = new DOMXpath($doc);
          $anchors = $xpath->query('//div[contains(@class, "post-inner")]//a');
          foreach ($anchors as $a) {
          $a->setAttribute('href', 'https://shortdomain.com/api?p=' . $a->attributes->getNamedItem('href')->nodeValue);
          }
          echo $doc->saveHTML();


          Output:



          <div class="post-inner"><div class="entry"><p><strong>
          <a href="https://shortdomain.com/api?p=http://example.net">link</a>
          </strong></p></div></div>


          Demo on 3v4l.org






          share|improve this answer

























            up vote
            3
            down vote










            up vote
            3
            down vote









            This is a good task for PHP's built-in DOMDocument and DOMXpath classes. Using xpath ensures that you only change anchors inside the div that you want.



            $main = '<div class="post-inner"><div class="entry"><p><strong><a href="http://example.net">link</a></strong></p></div></div>';
            $doc = new DOMDocument();
            $doc->loadHTML($main, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
            $xpath = new DOMXpath($doc);
            $anchors = $xpath->query('//div[contains(@class, "post-inner")]//a');
            foreach ($anchors as $a) {
            $a->setAttribute('href', 'https://shortdomain.com/api?p=' . $a->attributes->getNamedItem('href')->nodeValue);
            }
            echo $doc->saveHTML();


            Output:



            <div class="post-inner"><div class="entry"><p><strong>
            <a href="https://shortdomain.com/api?p=http://example.net">link</a>
            </strong></p></div></div>


            Demo on 3v4l.org






            share|improve this answer














            This is a good task for PHP's built-in DOMDocument and DOMXpath classes. Using xpath ensures that you only change anchors inside the div that you want.



            $main = '<div class="post-inner"><div class="entry"><p><strong><a href="http://example.net">link</a></strong></p></div></div>';
            $doc = new DOMDocument();
            $doc->loadHTML($main, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
            $xpath = new DOMXpath($doc);
            $anchors = $xpath->query('//div[contains(@class, "post-inner")]//a');
            foreach ($anchors as $a) {
            $a->setAttribute('href', 'https://shortdomain.com/api?p=' . $a->attributes->getNamedItem('href')->nodeValue);
            }
            echo $doc->saveHTML();


            Output:



            <div class="post-inner"><div class="entry"><p><strong>
            <a href="https://shortdomain.com/api?p=http://example.net">link</a>
            </strong></p></div></div>


            Demo on 3v4l.org







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 2:13

























            answered Nov 21 at 2:04









            Nick

            19.7k51434




            19.7k51434






















                up vote
                1
                down vote













                You can use the attribute selector with jquery to match all desired elements and then use the method 'attr' to change the href attribute.



                $("#the-post > div.post-inner > div.entry > p > strong > a[href="http://example.net/"]")
                .attr("href", "https://shortdomain.com/api?p=http://example.net/");


                I didn't really test it, but it should work just fine.






                share|improve this answer

























                  up vote
                  1
                  down vote













                  You can use the attribute selector with jquery to match all desired elements and then use the method 'attr' to change the href attribute.



                  $("#the-post > div.post-inner > div.entry > p > strong > a[href="http://example.net/"]")
                  .attr("href", "https://shortdomain.com/api?p=http://example.net/");


                  I didn't really test it, but it should work just fine.






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You can use the attribute selector with jquery to match all desired elements and then use the method 'attr' to change the href attribute.



                    $("#the-post > div.post-inner > div.entry > p > strong > a[href="http://example.net/"]")
                    .attr("href", "https://shortdomain.com/api?p=http://example.net/");


                    I didn't really test it, but it should work just fine.






                    share|improve this answer












                    You can use the attribute selector with jquery to match all desired elements and then use the method 'attr' to change the href attribute.



                    $("#the-post > div.post-inner > div.entry > p > strong > a[href="http://example.net/"]")
                    .attr("href", "https://shortdomain.com/api?p=http://example.net/");


                    I didn't really test it, but it should work just fine.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 2:08









                    Pedro Lima

                    13416




                    13416






















                        up vote
                        1
                        down vote













                        If you are doing this client-side, it's pretty painless with javascript's querySelectorAll:






                        let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
                        as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

                        <a href="http://example.net/">Don't change</a>
                        <div id="the-post">
                        <div class="post-inner">
                        <div class="entry">
                        <p>
                        change these:
                        <strong>
                        <a href="http://example2.net/">some inside </a> <br />
                        <a href="http://example3.net/">some other inside </a>
                        </strong></p>
                        </div>
                        </div>
                        </div>








                        share|improve this answer



























                          up vote
                          1
                          down vote













                          If you are doing this client-side, it's pretty painless with javascript's querySelectorAll:






                          let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
                          as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

                          <a href="http://example.net/">Don't change</a>
                          <div id="the-post">
                          <div class="post-inner">
                          <div class="entry">
                          <p>
                          change these:
                          <strong>
                          <a href="http://example2.net/">some inside </a> <br />
                          <a href="http://example3.net/">some other inside </a>
                          </strong></p>
                          </div>
                          </div>
                          </div>








                          share|improve this answer

























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            If you are doing this client-side, it's pretty painless with javascript's querySelectorAll:






                            let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
                            as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

                            <a href="http://example.net/">Don't change</a>
                            <div id="the-post">
                            <div class="post-inner">
                            <div class="entry">
                            <p>
                            change these:
                            <strong>
                            <a href="http://example2.net/">some inside </a> <br />
                            <a href="http://example3.net/">some other inside </a>
                            </strong></p>
                            </div>
                            </div>
                            </div>








                            share|improve this answer














                            If you are doing this client-side, it's pretty painless with javascript's querySelectorAll:






                            let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
                            as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

                            <a href="http://example.net/">Don't change</a>
                            <div id="the-post">
                            <div class="post-inner">
                            <div class="entry">
                            <p>
                            change these:
                            <strong>
                            <a href="http://example2.net/">some inside </a> <br />
                            <a href="http://example3.net/">some other inside </a>
                            </strong></p>
                            </div>
                            </div>
                            </div>








                            let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
                            as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

                            <a href="http://example.net/">Don't change</a>
                            <div id="the-post">
                            <div class="post-inner">
                            <div class="entry">
                            <p>
                            change these:
                            <strong>
                            <a href="http://example2.net/">some inside </a> <br />
                            <a href="http://example3.net/">some other inside </a>
                            </strong></p>
                            </div>
                            </div>
                            </div>





                            let as = document.querySelectorAll('#the-post .post-inner .entry p strong a')
                            as.forEach(a => a.href ="https://shortdomain.com/api?p="+a.href)

                            <a href="http://example.net/">Don't change</a>
                            <div id="the-post">
                            <div class="post-inner">
                            <div class="entry">
                            <p>
                            change these:
                            <strong>
                            <a href="http://example2.net/">some inside </a> <br />
                            <a href="http://example3.net/">some other inside </a>
                            </strong></p>
                            </div>
                            </div>
                            </div>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 21 at 2:12

























                            answered Nov 21 at 2:07









                            Mark Meyer

                            29.9k32549




                            29.9k32549






















                                up vote
                                0
                                down vote













                                Get rid of the dummy website then.






                                $main = get_the_content_with_formatting();
                                $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);








                                share|improve this answer























                                • I might be a bit slow for not thinking of this..
                                  – John Doe
                                  Nov 21 at 2:11










                                • What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
                                  – miken32
                                  Nov 21 at 2:13












                                • then remove <a
                                  – ACD
                                  Nov 21 at 2:25















                                up vote
                                0
                                down vote













                                Get rid of the dummy website then.






                                $main = get_the_content_with_formatting();
                                $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);








                                share|improve this answer























                                • I might be a bit slow for not thinking of this..
                                  – John Doe
                                  Nov 21 at 2:11










                                • What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
                                  – miken32
                                  Nov 21 at 2:13












                                • then remove <a
                                  – ACD
                                  Nov 21 at 2:25













                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                Get rid of the dummy website then.






                                $main = get_the_content_with_formatting();
                                $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);








                                share|improve this answer














                                Get rid of the dummy website then.






                                $main = get_the_content_with_formatting();
                                $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);








                                $main = get_the_content_with_formatting();
                                $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);





                                $main = get_the_content_with_formatting();
                                $stripped = str_replace('href="', 'href="https://shortdomain.com/api?p=', $main);






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 21 at 2:25

























                                answered Nov 21 at 2:04









                                ACD

                                51510




                                51510












                                • I might be a bit slow for not thinking of this..
                                  – John Doe
                                  Nov 21 at 2:11










                                • What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
                                  – miken32
                                  Nov 21 at 2:13












                                • then remove <a
                                  – ACD
                                  Nov 21 at 2:25


















                                • I might be a bit slow for not thinking of this..
                                  – John Doe
                                  Nov 21 at 2:11










                                • What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
                                  – miken32
                                  Nov 21 at 2:13












                                • then remove <a
                                  – ACD
                                  Nov 21 at 2:25
















                                I might be a bit slow for not thinking of this..
                                – John Doe
                                Nov 21 at 2:11




                                I might be a bit slow for not thinking of this..
                                – John Doe
                                Nov 21 at 2:11












                                What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
                                – miken32
                                Nov 21 at 2:13






                                What happens with <a class="foo" href="...">. Or, for that matter, just with 2 spaces between "a and "href".
                                – miken32
                                Nov 21 at 2:13














                                then remove <a
                                – ACD
                                Nov 21 at 2:25




                                then remove <a
                                – ACD
                                Nov 21 at 2:25


















                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404252%2fplacing-text-before-every-a-href-appearance%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

                                Sphinx de Gizeh

                                Dijon

                                Langue