Regex pattern for exact html element with style and replace with random string

I'm newbie to stackoverflow

So i have html file that has lots of <span style="display:none;">some text</span>

Their amount exceeds like 100 or 200 . I want to replace the inner text of the span with random strings

But i got 2 problems , the first one is that i cant find the right regex pattern to find span with inline style="display:none"

But i'm able to get all spans text with /<span (.*?)>(.*?)<\/span>/ . And here comes the second problem . I'm using php and here's my code

    preg_match_all('/<span (.*?)>(.*?)<\/span>/', $file, $matches);
    foreach ($matches as $value) {
        $string = str_replace($value, RandomString(), $file);

so it changes the inner texts of ALL spans but at once , RandomString() is a function that generates random string , and after str_replace it returns the changed the string , but all span's value are the same , as i get my random string generator only works once and the str_replace() replaces all found matches

I thought about recursion

function rec($string)
        preg_match('/<span (.*?)>(.*?)<\/span>/', $string, $matches);
        if (!$matches) {
            return $string;
        foreach ($matches as $value) {
            $string = str_replace($value,RandomString(), $string);

But it returns 500 error...

So how can i replace the found matches step by step?

Regex is mandatory

Read more here:

Content Attribution

This content was originally published by pyos baran at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: