Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

replace string in htmlcode but not in htmltags

Last post 11-15-2007, 8:13 AM by harakiri81. 2 replies.
Sort Posts: Previous Next
  •  11-15-2007, 3:26 AM 36551

    replace string in htmlcode but not in htmltags

    hello,

    i need a regex that replaces in a html source code a special word (eg. orange) but not the word in htmltags (attributes):

     <html>
        <head></head>
        <body>
            this is a text without any tags! replace please the following word: orange<br>               
            this is the number twentyone, this is the number twentytwo, this is the orange number twentythree!
            <image src="http://www.google.de/orange.gif" title="this is an orange" border="0"/>        
            <a href="http://www.google.de/orange.php">this is a link to a orange website, please do only replace the link text but not the uri!</a>
        </body>
    </html>

     

     my first attempt was this regex:

    (\>| |[\(])(orange)([\.\!\)\?\:\,]+| )(\>| |[\(])(orange)([\.\!\)\?\:\,]+| )


    does not work in all conditions, would be great if someone could help me!
    thanks in advance, roland           

     

  •  11-15-2007, 4:30 AM 36554 in reply to 36551

    Re: replace string in htmlcode but not in htmltags

    i have found in another thread this solution:

    (?<!<[^<>]*)orange(?![^<>]*>)

    this works in my regexsoftware "RegexDesigner.net" but not in my php code:

    <?php
    $replacement = "ananas";
    $searchtext = "orange";
    $searchpattern = "(?<!<[^<>]*)orange(?![^<>]*>)";
    $result = eregi_replace($searchpattern, $replacement, $searchtext)

    echo $result;
    ?>

    this brings me this error message from the php engine:
    Warning: eregi_replace(): REG_BADRPT in /srv/www/htdocs/t3_flad_intranet/typo3conf/ext/page_php_content/pi1/test.php(102) : eval()'d code on line 6

    is this normal? thanks
     






  •  11-15-2007, 8:13 AM 36562 in reply to 36554

    Re: replace string in htmlcode but not in htmltags

    in this way i found a solution:

     
    <?php

    // nur ein Beispiel:
    $heuhaufen = '<a href="www.google.de/banane.aspx">ich bin eine banane, ich bin eine ananas!</a> ich bin kein tag ich bin kein tag <img src="www.google.de/banana.jpg" title="banane">';
    $muster = '/banane(?=[^>]*<[^>]+>)/i';
    $ersatz = 'ananas';
    $ergebnis = preg_replace($muster, $ersatz, $heuhaufen);

    echo $ergebnis;

    ?> 

View as RSS news feed in XML