preg_replace to exclude PHP












2














Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is:



$newstring2 = preg_replace("/p{L}*?".preg_quote($match[$i])."p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);


Only problem with this is, that I need to exclude any keywords inside <a href='https://keyword.cz' title="keyword">keyword</a>



This is what I found https://stackoverflow.com/a/22821650/4928816



So is here someone who can help me merge this two regex together?



Example:



$text = 'this is sample text about something what is text.'
$keyword = 'text'


Now thanks to my regex I get:



$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


But If text is :



$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


This is what for example I get:



$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'


Update:
Why do I need this.
Working on function to replace all keywords with specific URL in specific blog post full of tags..
For examle if



$keyword = 'key';


I need to find and replace full world with a href tag, for example:
Key, Keyword, keyword, keylock, mykey, keys or also KeY, Keyword with UNICODE support










share|improve this question
























  • Can you show us the expected outputs for some given inputs?
    – Cid
    Nov 20 at 15:22






  • 3




    Is there a reason you're not using a DOM parser?
    – CD001
    Nov 20 at 15:22










  • Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps. It should be possible to present a HTML file that will be matched wrongly by any regular expression.
    – Bogdan N.
    Nov 20 at 15:30










  • See also: PHP Regular expression to match keyword outside HTML tag, Regex ignore URL already in HTML tags and php regex to match outside of html tags
    – mario
    Nov 20 at 15:31












  • Ok @Cid I added how Im using this,
    – Coxii
    Nov 20 at 16:41


















2














Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is:



$newstring2 = preg_replace("/p{L}*?".preg_quote($match[$i])."p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);


Only problem with this is, that I need to exclude any keywords inside <a href='https://keyword.cz' title="keyword">keyword</a>



This is what I found https://stackoverflow.com/a/22821650/4928816



So is here someone who can help me merge this two regex together?



Example:



$text = 'this is sample text about something what is text.'
$keyword = 'text'


Now thanks to my regex I get:



$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


But If text is :



$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


This is what for example I get:



$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'


Update:
Why do I need this.
Working on function to replace all keywords with specific URL in specific blog post full of tags..
For examle if



$keyword = 'key';


I need to find and replace full world with a href tag, for example:
Key, Keyword, keyword, keylock, mykey, keys or also KeY, Keyword with UNICODE support










share|improve this question
























  • Can you show us the expected outputs for some given inputs?
    – Cid
    Nov 20 at 15:22






  • 3




    Is there a reason you're not using a DOM parser?
    – CD001
    Nov 20 at 15:22










  • Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps. It should be possible to present a HTML file that will be matched wrongly by any regular expression.
    – Bogdan N.
    Nov 20 at 15:30










  • See also: PHP Regular expression to match keyword outside HTML tag, Regex ignore URL already in HTML tags and php regex to match outside of html tags
    – mario
    Nov 20 at 15:31












  • Ok @Cid I added how Im using this,
    – Coxii
    Nov 20 at 16:41
















2












2








2







Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is:



$newstring2 = preg_replace("/p{L}*?".preg_quote($match[$i])."p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);


Only problem with this is, that I need to exclude any keywords inside <a href='https://keyword.cz' title="keyword">keyword</a>



This is what I found https://stackoverflow.com/a/22821650/4928816



So is here someone who can help me merge this two regex together?



Example:



$text = 'this is sample text about something what is text.'
$keyword = 'text'


Now thanks to my regex I get:



$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


But If text is :



$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


This is what for example I get:



$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'


Update:
Why do I need this.
Working on function to replace all keywords with specific URL in specific blog post full of tags..
For examle if



$keyword = 'key';


I need to find and replace full world with a href tag, for example:
Key, Keyword, keyword, keylock, mykey, keys or also KeY, Keyword with UNICODE support










share|improve this question















Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is:



$newstring2 = preg_replace("/p{L}*?".preg_quote($match[$i])."p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);


Only problem with this is, that I need to exclude any keywords inside <a href='https://keyword.cz' title="keyword">keyword</a>



This is what I found https://stackoverflow.com/a/22821650/4928816



So is here someone who can help me merge this two regex together?



Example:



$text = 'this is sample text about something what is text.'
$keyword = 'text'


Now thanks to my regex I get:



$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


But If text is :



$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'


This is what for example I get:



$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'


Update:
Why do I need this.
Working on function to replace all keywords with specific URL in specific blog post full of tags..
For examle if



$keyword = 'key';


I need to find and replace full world with a href tag, for example:
Key, Keyword, keyword, keylock, mykey, keys or also KeY, Keyword with UNICODE support







php regex replace preg-replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 17:32

























asked Nov 20 at 15:18









Coxii

277




277












  • Can you show us the expected outputs for some given inputs?
    – Cid
    Nov 20 at 15:22






  • 3




    Is there a reason you're not using a DOM parser?
    – CD001
    Nov 20 at 15:22










  • Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps. It should be possible to present a HTML file that will be matched wrongly by any regular expression.
    – Bogdan N.
    Nov 20 at 15:30










  • See also: PHP Regular expression to match keyword outside HTML tag, Regex ignore URL already in HTML tags and php regex to match outside of html tags
    – mario
    Nov 20 at 15:31












  • Ok @Cid I added how Im using this,
    – Coxii
    Nov 20 at 16:41




















  • Can you show us the expected outputs for some given inputs?
    – Cid
    Nov 20 at 15:22






  • 3




    Is there a reason you're not using a DOM parser?
    – CD001
    Nov 20 at 15:22










  • Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps. It should be possible to present a HTML file that will be matched wrongly by any regular expression.
    – Bogdan N.
    Nov 20 at 15:30










  • See also: PHP Regular expression to match keyword outside HTML tag, Regex ignore URL already in HTML tags and php regex to match outside of html tags
    – mario
    Nov 20 at 15:31












  • Ok @Cid I added how Im using this,
    – Coxii
    Nov 20 at 16:41


















Can you show us the expected outputs for some given inputs?
– Cid
Nov 20 at 15:22




Can you show us the expected outputs for some given inputs?
– Cid
Nov 20 at 15:22




3




3




Is there a reason you're not using a DOM parser?
– CD001
Nov 20 at 15:22




Is there a reason you're not using a DOM parser?
– CD001
Nov 20 at 15:22












Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps. It should be possible to present a HTML file that will be matched wrongly by any regular expression.
– Bogdan N.
Nov 20 at 15:30




Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps. It should be possible to present a HTML file that will be matched wrongly by any regular expression.
– Bogdan N.
Nov 20 at 15:30












See also: PHP Regular expression to match keyword outside HTML tag, Regex ignore URL already in HTML tags and php regex to match outside of html tags
– mario
Nov 20 at 15:31






See also: PHP Regular expression to match keyword outside HTML tag, Regex ignore URL already in HTML tags and php regex to match outside of html tags
– mario
Nov 20 at 15:31














Ok @Cid I added how Im using this,
– Coxii
Nov 20 at 16:41






Ok @Cid I added how Im using this,
– Coxii
Nov 20 at 16:41














2 Answers
2






active

oldest

votes


















1














If it must be done with regex I think PCRE verbs are your best option. Exclude all links then search for the term with word boundaries.



<a[Ss]+?</a>(*SKIP)(*FAIL)|bTERMb


Demo: https://regex101.com/r/KlE1kc/1/



an example of a flaw with this though is if the a ever had a </a> in it. e.g. onclick='write("</a>")' a parser is really the best approach. There are a lot of gotchas with HTML and regexs.






share|improve this answer





























    3














    How about this with negative lookahead. Regex



    Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have </a> after it.



    $re = '/(text)(?!</a>)/m';
    $str = 'this is sample text about something what is text.

    this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.';
    $subst = '<a href='somelink.php'>$1</a>';

    $result = preg_replace($re, $subst, $str);

    echo $result;


    Output:



    this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>. 

    this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.


    DEMO: https://3v4l.org/DVTB1






    share|improve this answer





















    • This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
      – user3783243
      Nov 20 at 16:09










    • that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
      – Coxii
      Nov 20 at 16:13













    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%2f53396138%2fpreg-replace-to-exclude-a-href-a-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














    If it must be done with regex I think PCRE verbs are your best option. Exclude all links then search for the term with word boundaries.



    <a[Ss]+?</a>(*SKIP)(*FAIL)|bTERMb


    Demo: https://regex101.com/r/KlE1kc/1/



    an example of a flaw with this though is if the a ever had a </a> in it. e.g. onclick='write("</a>")' a parser is really the best approach. There are a lot of gotchas with HTML and regexs.






    share|improve this answer


























      1














      If it must be done with regex I think PCRE verbs are your best option. Exclude all links then search for the term with word boundaries.



      <a[Ss]+?</a>(*SKIP)(*FAIL)|bTERMb


      Demo: https://regex101.com/r/KlE1kc/1/



      an example of a flaw with this though is if the a ever had a </a> in it. e.g. onclick='write("</a>")' a parser is really the best approach. There are a lot of gotchas with HTML and regexs.






      share|improve this answer
























        1












        1








        1






        If it must be done with regex I think PCRE verbs are your best option. Exclude all links then search for the term with word boundaries.



        <a[Ss]+?</a>(*SKIP)(*FAIL)|bTERMb


        Demo: https://regex101.com/r/KlE1kc/1/



        an example of a flaw with this though is if the a ever had a </a> in it. e.g. onclick='write("</a>")' a parser is really the best approach. There are a lot of gotchas with HTML and regexs.






        share|improve this answer












        If it must be done with regex I think PCRE verbs are your best option. Exclude all links then search for the term with word boundaries.



        <a[Ss]+?</a>(*SKIP)(*FAIL)|bTERMb


        Demo: https://regex101.com/r/KlE1kc/1/



        an example of a flaw with this though is if the a ever had a </a> in it. e.g. onclick='write("</a>")' a parser is really the best approach. There are a lot of gotchas with HTML and regexs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 16:18









        user3783243

        2,21911021




        2,21911021

























            3














            How about this with negative lookahead. Regex



            Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have </a> after it.



            $re = '/(text)(?!</a>)/m';
            $str = 'this is sample text about something what is text.

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.';
            $subst = '<a href='somelink.php'>$1</a>';

            $result = preg_replace($re, $subst, $str);

            echo $result;


            Output:



            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>. 

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.


            DEMO: https://3v4l.org/DVTB1






            share|improve this answer





















            • This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
              – user3783243
              Nov 20 at 16:09










            • that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
              – Coxii
              Nov 20 at 16:13


















            3














            How about this with negative lookahead. Regex



            Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have </a> after it.



            $re = '/(text)(?!</a>)/m';
            $str = 'this is sample text about something what is text.

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.';
            $subst = '<a href='somelink.php'>$1</a>';

            $result = preg_replace($re, $subst, $str);

            echo $result;


            Output:



            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>. 

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.


            DEMO: https://3v4l.org/DVTB1






            share|improve this answer





















            • This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
              – user3783243
              Nov 20 at 16:09










            • that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
              – Coxii
              Nov 20 at 16:13
















            3












            3








            3






            How about this with negative lookahead. Regex



            Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have </a> after it.



            $re = '/(text)(?!</a>)/m';
            $str = 'this is sample text about something what is text.

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.';
            $subst = '<a href='somelink.php'>$1</a>';

            $result = preg_replace($re, $subst, $str);

            echo $result;


            Output:



            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>. 

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.


            DEMO: https://3v4l.org/DVTB1






            share|improve this answer












            How about this with negative lookahead. Regex



            Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have </a> after it.



            $re = '/(text)(?!</a>)/m';
            $str = 'this is sample text about something what is text.

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.';
            $subst = '<a href='somelink.php'>$1</a>';

            $result = preg_replace($re, $subst, $str);

            echo $result;


            Output:



            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>. 

            this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.


            DEMO: https://3v4l.org/DVTB1







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 15:37









            Curious_Mind

            14.5k32443




            14.5k32443












            • This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
              – user3783243
              Nov 20 at 16:09










            • that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
              – Coxii
              Nov 20 at 16:13




















            • This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
              – user3783243
              Nov 20 at 16:09










            • that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
              – Coxii
              Nov 20 at 16:13


















            This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
            – user3783243
            Nov 20 at 16:09




            This has the same effect though if an attribute has the value, regex101.com/r/oRVBvi/2
            – user3783243
            Nov 20 at 16:09












            that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
            – Coxii
            Nov 20 at 16:13






            that may be work, but this preg_replace must exclude href and match any FULL keyword, with no difference. between Keyword Keywords, keyword mykeyword. So do you think, that what you post can be used in regex I gave above? I need something like this: $newstring1 = preg_replace("/p{L}*?/(text)(?!</a>)p{L}*/ui", "<a href='google.com' class='link'>$0</a>", $newstring);
            – Coxii
            Nov 20 at 16:13




















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53396138%2fpreg-replace-to-exclude-a-href-a-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

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Fotorealismo