如何使用正则表达式获得HTML标签(嵌套/非嵌套)文本?



我试图从使用搜索词的网页中获得所有文本,而不是HTML标签的任何属性,然后试图用其他文本替换它们。正则表达式$pattern1 = "/>([^<]*)

即使它工作,我有一个问题集成它与搜索词,如$模式变量。

代码如下:

<?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";
}

如果您确实需要使用正则表达式,这可能会有所帮助。

<?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);

这是工作代码:

<?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>";

}
?>

最新更新