Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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正则表达式问题_Php_Regex - Fatal编程技术网

PHP正则表达式问题

PHP正则表达式问题,php,regex,Php,Regex,我想替换这一行: <img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973"> 为此: <p align="center"><img width="600" height="256" alt="javascript00" s

我想替换这一行:

<img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973">

为此:

<p align="center"><img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00"></p>

通过使用简单的regexp。仅当img类包含aligncenter:时,它包括删除图像类并在周围添加

谢谢你的帮助! 圣诞快乐:)

解决方案:

$result = preg_replace('#<img([^>]*?)?\s+class="[^"]*aligncenter[^"]*"\s*([^>]*?)>#', '<p align="center"><img$1 $2></p>', $data);
$result=preg_replace('#]*?)?\s+class=“[^”]*aligncenter[^”]*”\s*([^>]*?)>#','

,$data);
虽然不建议使用正则表达式解析和操作HTML,但这应该是可行的

<?php
$in = '<img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973">';

$out = preg_replace(
    '@<img( [^>]*?)\s*class="[^"]*"([^>]*?)>@', 
    '<p align="center"><img $1$2></p>', 
    $in
);

// if you need the image's class to be replaced with one class:
$out = preg_replace(
    '@<img( [^*]+?)\s*class="[^"]*"([^>]*?)>@', 
    '<p align="center"><img class="aligncenter" $1$2></p>', 
    $in
);

如果您仅为img标记需要此项,可以使用此regexp:

$dom = new DOMDocument;
// supress errors because DOMDocument will actually parse a malformed document
// even when it emits errors. this is something that is wrong with PHP. 
@$dom->loadHTML('<img src="foo" class="bar">');
$xp = new DOMXPath($dom);
$node = $xp->query('body/img')->item(0);
$node->removeAttribute('class');
echo $dom->saveXML($node).PHP_EOL;
$data='';
$result=preg_replace('#]*?)\s+class=“[^”]*”\s*([^>]*?)>#',“

”,$data); 回声$结果;
与我的答案几乎相同,8分钟后。为什么?@shabberobe当我看到你的答案时,regexp是错误的,其余的都不在那里。而且,键入答案需要时间,所以提交答案的时间不是我开始写答案的时候。好吧,酷,我理解。虽然我不同意最初的regex是错误的,只是范围更广。当然y处理了OP的问题。@StatsM:HUMM不使用以下语法:
@StatsM额外空间?img中的标记之间只有一个空格。正则表达式中的问题是类必须至少是第二个标记。第一个标记什么时候像
一样不起作用。我不想要
class=“aligncenter”“
从我的img标签中,我想在包含此类的每个img标签周围添加

,并删除此标签。要继续,请删除img标记的类并添加

:)您的第一个正则表达式不能使用以下语法:
  $data = '<img width="600" height="256" alt="javascript00" src="http://localhost/img/test.png" title="javascript00" class="aligncenter size-full wp-image-1973">';
  $result = preg_replace('#<img( [^>]*?)\s+class="[^"]*"\s*([^>]*?)>#', '<p align="center"><img $1$2></p>', $data);
  echo $result;