Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
How to get HTML tags ( Nested/ Non Nested) texts using regex?

I am trying to get all the texts not any attributes from HTML tags from a webpage using a search term and then trying to replace them with some other text. My regular expression $pattern1 = "/>([^<]*)</g" for this which gets the text from the tags. Though it isn't working here but it works here https://regex101.com/r/bExomf/2

Even if it works, I am having a problem integrating it with the search term like $pattern variable.

Here below is the code:

<?php
    $file = 'j2-regex.html';
    $searchfor = 'J2';

    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');

    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($file);
    // escape special characters in the query
    $pattern = preg_quote($searchfor, '/');
    // finalise the regular expression, matching the whole line
    $pattern = "/^.*$pattern.*\$/m";
    $pattern1 = ">[^<]*</g";
    // search, and store all matching occurences in $matches
    if(preg_match_all($pattern, $contents, $matches)){
        echo "Found matches:\n";
        
        echo implode("\n", $matches[0]);
        
        
        // Old string
        $str_old = $searchfor;
        
        // New string
        $str_new = "J<sup>2</sup>";
        
        // Replacing part of string
        $final_str = str_ireplace($str_old,$str_new,$contents);
        
        echo("Modified String : ");
        echo($final_str);

    }
    else{
        echo "No matches found";
    }

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Maybe this helps a bit, if you really have to use regex.

<?php
    $file = 'j2-regex.html';
    $searchfor = 'J2';
    $str_new = "J<sup>2</sup>";
    $regex = "/<.*?>(*SKIP)(*FAIL)|" . $searchfor . "/";

    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');

    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($file);
    $final_str = preg_replace($regex, $str_new, $contents);
        
    echo("Modified String : ");
    echo($final_str);
about 4 years ago · Juan Pablo Isaza Relatório

0

This is the working code:

<?php
    $file = '../j2-regex.html';
    $searchfor  = $_POST['searchTerm'];
    $replaceTerm  = $_POST['replaceTerm'];


    $regex = "~<.*?>(*SKIP)(*FAIL)|" . $searchfor . "~";

    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');

    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($file);


    if(preg_match_all($regex, $contents)){

        echo $msg = "Matches Founded & File Replaced <a class='btn btn-success' target='_blank' href='j2-regex.html' style='float: right;bottom: 9px;position: relative;'>Visit Webpage</a>";

        
        // // Replacing part of string
        $final_str = preg_replace($regex, $replaceTerm, $contents);
        
        // echo("Modified String : ");
        // echo($final_str);

        $myfile = fopen("../j2-regex.html", "w") or die("Unable to open file!");

        fwrite($myfile, $final_str);
        fclose($myfile);

        // file_put_contents($file , $final_str, FILE_APPEND | LOCK_EX);

    }
    else{
        
        echo $msg = "No Matches Founded <a class='btn btn-success' target='_blank' href='j2-regex.html' style='float: right;bottom: 9px;position: relative;'>Visit Webpage</a>";


    }

?>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda