Viewed   641 times

I am trying to replace text between two tags in a html document. I want to replace any text that isn't enclosed with a < and >. I want to use str_replace to do this.

php $string = '<html><h1> some text i want to replace</h1><p>some stuff i want to replace </p>';

$text_to_echo = str_replace("Bla","Da",$String);
echo $text_to_echo;

 Answers

1

Try this:

    <?php

$string = '<html><h1> some text i want to replace</h1><p>
    some stuff i want to replace </p>';
$text_to_echo =  preg_replace_callback(
    "/(<([^.]+)>)([^<]+)(<\/\2>)/s", 
    function($matches){
        /*
         * Indexes of array:
         *    0 - full tag
         *    1 - open tag, for example <h1>
         *    2 - tag name h1
         *    3 - content
         *    4 - closing tag
         */
        // print_r($matches);
        $text = str_replace(
           array("text", "want"), 
           array('TEXT', 'need'),
                $matches[3]
        );
        return $matches[1].$text.$matches[4];
    }, 
    $string
);
echo $text_to_echo;
Monday, November 7, 2022
 
wolphin
 
1

(?<!,)s

That uses a negative lookbehind to match all spaces (s) that aren't followed by a ,.

preg_replace("/(?<!,)s/", '-', $mystring);

Play with the regex here.

Thursday, October 20, 2022
 
marcg
 
1

How can I avoid that? I want it to be check-out-the-1-place - so that there only is one hyphen between each word. Here is my code:

Whilst Mohammad's answer is nearly there, here is a more fully working PCRE regex method and explanation as to how it works, so you can use it as you need:

$str = trim(strtolower($pathname));
$newStr = preg_replace('/[s.,-]+/', '-', $str);

How this works:

  • Match a single character present in the list below [s.,-]+
    • + Quantifier Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    • s matches any whitespace character (equal to [rntfv])
    • .,- matches a single character in the list .,- (case sensitive)
    • The dash - must come at the end of the [] set.

Results:

This: check out the 1. place

Becomes:

check-out-the-1-place

And

This: check out the - 1. place

Becomes

check-out-the-1-place


Further:

I would go further and assuming you are using this for a URL slug (a what?!); strip out all non-alphanumeric characters from the string and replace with a single - as per typical website slugs.

 $newStr = preg_replace('/[^a-z0-9]+/i', '-', $str);

How this works:

  • Match a single character NOT (^) present in the list below [a-z0-9]+
    • + Quantifier Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    • a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
    • 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
    • The i at the end indicates the judgements are case In-sensitive.

Example:

check out - the no.! 1. Place

Becomes:

check-out-the-1-Place

Tuesday, August 30, 2022
1
$string = '< html>
< p> blah blah blah foo try foo< /p>
< pre> foo try foo word foofoo < /pre>
< /html>';
$string = preg_replace('/foo(?=(?:.(?!< pre>))*< /pre>)/Um', 'boo', $string);
echo $string;
Tuesday, September 20, 2022
2

If DreamWeaver regex supports look-ahead assertions:

</title>((?:(?!</head>)[sS])*)</head>

The usual warning "don't work on HTML with regex, this will fail at some point" applies. The points where this would fail could be:

<script>var str = "something that contains </head>";<script>

or

<!-- a comment that refers to </head> -->

among other constellations.

Tuesday, October 4, 2022
 
chovy
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :