preg_replace_回调错误中的正则表达式。PHP

preg_replace_回调错误中的正则表达式。PHP,php,mysql,regex,str-replace,preg-replace-callback,Php,Mysql,Regex,Str Replace,Preg Replace Callback,我试图创建一个web应用程序,将任何选定的网页转换为简单的英语形式。我在My_SQL数据库中存储了逐字翻译。到目前为止,我有这个代码。它的工作,但似乎只是做什么,我想它在几个标签,而不是整个页面太多。我想这可能是由于正则表达式错误 <? $English = array(); $Simple = array(); $con = mysqli_connect("localhost","root","root","Words"); $getmodels = my

我试图创建一个web应用程序,将任何选定的网页转换为简单的英语形式。我在My_SQL数据库中存储了逐字翻译。到目前为止,我有这个代码。它的工作,但似乎只是做什么,我想它在几个标签,而不是整个页面太多。我想这可能是由于正则表达式错误

<?
    $English = array();
    $Simple = array();
    $con = mysqli_connect("localhost","root","root","Words");
    $getmodels = mysqli_query($con, "SELECT * FROM Wordsweb");
    while($res = mysqli_fetch_assoc($getmodels)) {
        $English[] = $res['English'];
        $Simple[] = $res['Simple'];
    }
    $url = $_GET['url'];
    $string = file_get_contents($url);
    $text_to_echo =  preg_replace_callback(
        "/(<([^.]+)>)([^<]+)(<\\/\\2>)/s", 
        function($matches) use ($English, $Simple) {
            /*
             * Indexes of array:
             *    0 - full tag
             *    1 - open tag, for example <h1>
             *    2 - tag name h1
             *    3 - content
             *    4 - closing tag
             */
            $matches[3] = strtolower($matches[3]);
            $text = str_replace($English, $Simple, $matches[3]);
            return $matches[1].$text.$matches[4];
        }, 
        $string
    );
    echo "<base href=\"" . $url . "/\" />";
    echo $text_to_echo;
    ?>

您可以使用DOM+Xpath获取和更改HTML文档中的文本节点:

$html = <<<'HTML'
  <html>
    <body>
      <h1>Hello World!</h1>
      <div>
        <p>Lorem Ipsum...</p>
      </div>
    </body>
  </html>
HTML;

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);

$nodes = $xpath->evaluate("//text()");
foreach ($nodes as $node) {
  $node->nodeValue = strToUpper($node->nodeValue);
}

echo $dom->saveHtml();
$html=nodeValue=strToUpper($node->nodeValue);
}
echo$dom->saveHtml();

您的正则表达式的这一部分:
()
应该是开头标记,但它并不能满足您的期望<代码>[^.]+尤其会匹配一个或多个非点的内容,因此它将匹配比单个标记内容多得多的内容。一般来说,您不应该使用正则表达式来解析URL。我想改用DOM,但我不知道如何在这种情况下实现DOM!或者,您可以使用。您应该更改此
/()([^I将尝试使用。假设*表示所有标记是否正确