Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用preg_replace替换字符集_Php_Replace - Fatal编程技术网

Php 用preg_replace替换字符集

Php 用preg_replace替换字符集,php,replace,Php,Replace,我有不同的网站内容存储在名为$content的variabel中。现在我想做的是在内容中搜索元标记,如下所示: <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> $content = preg_replace('/(charset=)(.+)\"/', "$1"."ISO-8859-1", $content); 然后将utf-8更换为IS0-8859-1。如何使用preg_替换来实现这一

我有不同的网站内容存储在名为$content的variabel中。现在我想做的是在内容中搜索元标记,如下所示:

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
$content = preg_replace('/(charset=)(.+)\"/', "$1"."ISO-8859-1", $content);

然后将utf-8更换为IS0-8859-1。如何使用preg_替换来实现这一点


请注意,每一次出现都与meta标记不同。它可能会有所不同,这取决于您获取的网站。

您不需要使用
preg\u replace
来执行此操作。只需使用:


像这样的东西怎么样:

$input = 'sometext<meta http-equiv="Content-type" content="text/html; charset=utf-8" />someothertext';

$output = preg_replace('#<meta http-equiv="Content-type" content="text/html; charset=(utf-8)" />#', 
    '<meta http-equiv="Content-type" content="text/html; charset=IS0-8859-1" />', 
    $input);

var_dump($output);
$output = str_replace('<meta http-equiv="Content-type" content="text/html; charset=utf-8" />', 
    '<meta http-equiv="Content-type" content="text/html; charset=IS0-8859-1" />', 
    $input);
var_dump($output);
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body>
    <p>Hello, world!</p>
</body>
</html>
这将获得相同的输出:

string 'sometext<meta http-equiv="Content-type" content="text/html; charset=IS0-8859-1" />someothertext' (length=95)
最有趣的部分是:

  • 您正在使用DOM解析器和标准DOM方法
  • 您可以执行XPath查询以准确定位所需的元素

生成的HTML将如下所示:

$input = 'sometext<meta http-equiv="Content-type" content="text/html; charset=utf-8" />someothertext';

$output = preg_replace('#<meta http-equiv="Content-type" content="text/html; charset=(utf-8)" />#', 
    '<meta http-equiv="Content-type" content="text/html; charset=IS0-8859-1" />', 
    $input);

var_dump($output);
$output = str_replace('<meta http-equiv="Content-type" content="text/html; charset=utf-8" />', 
    '<meta http-equiv="Content-type" content="text/html; charset=IS0-8859-1" />', 
    $input);
var_dump($output);
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body>
    <p>Hello, world!</p>
</body>
</html>

测验
你好,世界

可能有点重,需要更多的代码。。。但是,有了它,它应该总是可以工作的(好吧,只要用作输入的HTML不是太混乱,我想)

它将适用于文档中的任何其他内容;-)


也许这对你来说有点太多了,但是,幸运的是,你会在解析HTML的那天记住这一点,而不会与任何类型的变异正则表达式发生冲突^^


哦,当然,更改元内容类型不会更改内容的真正编码:如果需要,您仍然需要自己进行更改(例如,请参见或)


您可能还需要更改HTTP内容类型标头(如果/当设置HTTP标头时,不确定浏览器如何处理元数据)

您可以只匹配“charset=*”,并用“ISO-8859-1”替换*,不管它是什么

大概是这样的:

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
$content = preg_replace('/(charset=)(.+)\"/', "$1"."ISO-8859-1", $content);

阅读我在karim79的帖子上的评论::)这看起来是最简单的解决方案……但如果有,将会发生什么”;charset=utf-8“在页面中的其他位置?例如,如果在包含“;charset=utf-8“字符串在其内容中多次出现(在这个答案中,例如^^)?@Pascal MARTIN-然后可以使用一个更完整的字符串-查看我的答案;)谢谢您的时间,但您应该阅读我在其他帖子上的评论。因为每一次发生的事情都不是这样的……阅读其他帖子上的评论是好的;此时,只有一个其他答案,唯一的评论是“阅读我对karim79帖子的评论:“^^那么……好吧:-我想卡里姆79已经删除了他的答案^^;如果你的问题不是你在作品中问的,我们无法猜测它是什么;-)你应该编辑作品来问“完整”的问题;这样会更容易帮助你:-)我编辑我的答案是为了提供更多的信息;它可能有点“沉重”,但总有一天会有用的对不起,我还是不明白你在问什么。如果你只是想用ISO-8859-1替换UTF-8,那么我的答案会很好。是的,我测试过它,知道它很好用。但是为什么有些网站上的人物仍然很怪异呢?就像奥希奥一样,奥希奥很奇怪。它变成了Ã