Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
¿Cómo obtener textos de etiquetas HTML (anidados/no anidados) usando expresiones regulares?

Estoy tratando de obtener todos los textos, no ningún atributo de las etiquetas HTML de una página web usando un término de búsqueda y luego tratando de reemplazarlos con algún otro texto. Mi expresión regular $pattern1 = "/>([^<]*)</g" para esto que obtiene el texto de las etiquetas. Aunque no funciona aquí, pero funciona aquí https://regex101.com/r/bExomf/2

Incluso si funciona, tengo problemas para integrarlo con el término de búsqueda como variable de patrón $.

Aquí debajo está el código:

 <?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 Respuestas
Responde la pregunta

0

Tal vez esto ayude un poco, si realmente tiene que usar expresiones regulares.

 <?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 Denunciar

0

Este es el código de trabajo:

 <?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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda